我必须编写一个bash脚本,该脚本根据传递的命令行参数在后台启动进程,并返回它是否能够成功 运行 启动程序。
这是我想要实现的伪代码
if [ "$1" = "PROG_1" ] ; then
./launchProg1 &
if [ isLaunchSuccess ] ; then
echo "Success"
else
echo "failed"
exit 1
fi
elif [ "$1" = "PROG_2" ] ; then
./launchProg2 &
if [ isLaunchSuccess ] ; then
echo "Success"
else
echo "failed"
exit 1
fi
fi
脚本不能wait
或sleep
不能被另一个关键任务c ++程序调用,并且需要很高的吞吐量(每秒不启动任何进程),而且进程的运行时间未知。脚本既不需要捕获任何输入/输出,也不需要等待启动过程的完成。
我尝试以下操作失败:
#Method 1
if [ "$1" = "KP1" ] ; then
echo "The Arguement is KP1"
./kp 'this is text' &
if [ $? = "0" ] ; then
echo "Success"
else
echo "failed"
exit 1
fi
elif [ "$1" = "KP2" ] ; then
echo "The Arguement is KP2"
./NoSuchCommand 'this is text' &
if [ $? = "0" ] ; then
echo "Success"
else
echo "failed"
exit 1
fi
#Method 2
elif [ "$1" = "CD5" ] ; then
echo "The Arguement is CD5"
cd "doesNotExist" &
PROC_ID=$!
echo "PID is $PROC_ID"
if kill -0 "$PROC_ID" ; then
echo "Success"
else
echo "failed"
exit 1
fi
#Method 3
elif [ "$1" = "CD6" ] ; then
echo "The Arguement is CD6"
cd .. &
PROC_ID=$!
echo "PID is $PROC_ID"
ps -eo pid | grep "$PROC_ID" && { echo "Success"; exit 0; }
ps -eo pid | grep "$PROC_ID" || { echo "failed" ; exit 1; }
else
echo "Unknown Argument"
exit 1
fi
运行脚本会产生不可靠的输出。方法1和2总是返回,Success
而方法3failed
在检查之前进程执行完成时返回。
这是在GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
和测试的示例GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
[scripts]$ ./processStarted3.sh KP1
The Arguement is KP1
Success
[scripts]$ ./processStarted3.sh KP2
The Arguement is KP2
Success
./processStarted3.sh: line 13: ./NoSuchCommand: No such file or directory
[scripts]$ ./processStarted3.sh CD6
The Arguement is CD6
PID is 25050
failed
正如在类似问题中所建议的那样,我不能使用进程名称,因为一个进程可能会执行多次,而其他进程则无法应用。
我还没有尝试过screen和tmux,因为获得在生产服务器上安装它们的许可并不容易(但是如果剩下的唯一选择就是这样做)
UPDATE
@ghoti
./kp
是存在的程序,启动该程序将返回Success
。./NoSuchCommand
不存在。仍然可以从(编辑)输出中看到,脚本错误地返回Success
。
进程完成执行或程序异常终止无关紧要。不会以任何方式跟踪通过脚本启动的程序(因此,我们不存储pid
在任何表中,也没有必要使用deamontools
)。
@Etan赖斯纳
一个程序,它未能的实施例发射将是./NoSuchCommand
,它不存在。或者也许是一个损坏的程序无法启动。
@Vorsprung
调用在后台启动程序的脚本不会花费很多时间(并且可以根据我们的期望进行管理)。但是sleep 1
随着时间的推移会累积导致问题。
上述#Method3
工作的精细限制过程ps -eo pid | grep "$PROC_ID" && { echo "Success"; exit 0; }
可以在执行检查之前终止。
这是一个示例,该示例将显示过程的结果,无论该过程是否成功启动。
#!/bin/bash
$1 & #executes a program in background which is provided as an argument
pid=$! #stores executed process id in pid
count=$(ps -A| grep $pid |wc -l) #check whether process is still running
if [[ $count -eq 0 ]] #if process is already terminated, then there can be two cases, the process executed and stop successfully or it is terminated abnormally
then
if wait $pid; then #checks if process executed successfully or not
echo "success"
else #process terminated abnormally
echo "failed (returned $?)"
fi
else
echo "success" #process is still running
fi
#Note: The above script will only provide a result whether process started successfully or not. If porcess starts successfully and later it terminates abnormally then this sciptwill not provide a correct result
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句