Bash脚本来检查进程是否正在运行,并读取用户输入以在必要时启动。

时间:2021-12-12 15:45:45

Having a problem with using nohup in a script. The script works properly if nohup is not used to start the process. The following error is received when run:

在脚本中使用nohup有问题。如果不使用nohup启动进程,脚本将正常工作。运行时收到如下错误:

./iper.sh: line 16: syntax error near unexpected token `;'
./iper.sh: line 16: `        [Yy]*) nohup iperf -s > /dev/null 2>&1&; break;;'

Here is the full script:

以下是全文:

#!/bin/bash
    echo "Checking to see if Iperf is running:"
sleep 2

ps cax | grep iperf > /dev/null
if [ $? -eq 0 ]; then
    echo "Iperf is running."
else
    echo "Iperf is not running." 
fi
sleep 2

while true; do
    read -p "Press Y to start Iperf or N to exit: " yn
    case $yn in
        [Yy]*) nohup iperf -s > /dev/null 2>&1&; break;;
        [Nn]*) exit;; 
    esac
done    

What is happening?

发生了什么?

3 个解决方案

#1


2  

If you're going to terminate your command with & to put it to background, do not terminate it with another semicolon ; as well:

如果要用&结束命令,不要用另一个分号结束;:

[Yy]*) nohup iperf -s > /dev/null 2>&1& break;;

Previously

以前

2>&1&;

#2


0  

I guess you have an extra & in 2>&1&

我猜你在>和1&中有一个额外的&

Change it to 2>&1

2 > & 1改变它

#3


0  

Check with below script

检查下面的脚本

#!/bin/bash
    echo "Checking to see if Iperf is running:"
sleep 1

if  `pgrep iperf >/dev/null 2>&1`
then
    echo "iperf Running"
else
    echo "iperf not Running"
fi

sleep 1

while true; do
        echo "Do you wanna start Iperf (y/n)"
        read  -n 1 ch; p=`echo ${ch} | tr A-Z a-z`
        case $p in
                y)nohup iperf -s > /dev/null 2>&1 break;;

                n)exit;;

                *)continue;
    esac
done

when this been executed it waits for user to press*ctrl + c* to come out

当执行时,它等待用户按*ctrl + c*出来

if you are using 2>&1&(for continuing without user interference)allowing user to do other work

如果您正在使用2>和1& &(用于继续不受用户干扰),则允许用户进行其他工作

replace below line in y) condition

在y)状态下替换下面一行

nohup iperf -s > /dev/null 2>&1& break;;

#1


2  

If you're going to terminate your command with & to put it to background, do not terminate it with another semicolon ; as well:

如果要用&结束命令,不要用另一个分号结束;:

[Yy]*) nohup iperf -s > /dev/null 2>&1& break;;

Previously

以前

2>&1&;

#2


0  

I guess you have an extra & in 2>&1&

我猜你在>和1&中有一个额外的&

Change it to 2>&1

2 > & 1改变它

#3


0  

Check with below script

检查下面的脚本

#!/bin/bash
    echo "Checking to see if Iperf is running:"
sleep 1

if  `pgrep iperf >/dev/null 2>&1`
then
    echo "iperf Running"
else
    echo "iperf not Running"
fi

sleep 1

while true; do
        echo "Do you wanna start Iperf (y/n)"
        read  -n 1 ch; p=`echo ${ch} | tr A-Z a-z`
        case $p in
                y)nohup iperf -s > /dev/null 2>&1 break;;

                n)exit;;

                *)continue;
    esac
done

when this been executed it waits for user to press*ctrl + c* to come out

当执行时,它等待用户按*ctrl + c*出来

if you are using 2>&1&(for continuing without user interference)allowing user to do other work

如果您正在使用2>和1& &(用于继续不受用户干扰),则允许用户进行其他工作

replace below line in y) condition

在y)状态下替换下面一行

nohup iperf -s > /dev/null 2>&1& break;;