这是我想要实现的伪代码
if [ "" = "PROG_1" ] ; then ./launchProg1 & if [ isLaunchSuccess ] ; then echo "Success" else echo "Failed" exit 1 fielif [ "" = "PROG_2" ] ; then ./launchProg2 & if [ isLaunchSuccess ] ; then echo "Success" else echo "Failed" exit 1 fifi
脚本不能等待或睡眠,因为它将由另一个关键任务c程序调用,并且需要高吞吐量(每秒没有启动进程),而且进程的运行时间未知.脚本既不需要捕获任何输入/输出,也不需要等待已启动的进程完成.
我没有成功尝试以下方法:
#Method 1if [ "" = "KP1" ] ; then echo "The Arguement is KP1" ./kp 'this is text' & if [ $? = "0" ] ; then echo "Success" else echo "Failed" exit 1 fielif [ "" = "KP2" ] ; then echo "The Arguement is KP2" ./NoSuchCommand 'this is text' & if [ $? = "0" ] ; then echo "Success" else echo "Failed" exit 1 fi#Method 2elif [ "" = "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 3elif [ "" = "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 1fi
运行脚本会产生不可靠的输出.方法1,2总是返回成功,而方法3在检查之前完成流程执行时返回失败.
这是在GNU bash上测试的示例,版本4.1.2(1)-release(x86_64-redhat-linux-gnu)和GNU bash,版本4.3.11(1)-release(x86_64-pc-linux-gnu)
[scripts]$./processstarted3.sh KP1The Arguement is KP1Success[scripts]$./processstarted3.sh KP2The Arguement is KP2Success./processstarted3.sh: line 13: ./NoSuchCommand: No such file or directory[scripts]$./processstarted3.sh CD6The Arguement is CD6PID is 25050Failed
正如类似问题所示,我不能使用进程名称,因为one process may be executed several times和others无法应用.
我没有尝试过screen和tmux,因为获得在生产服务器上安装它们的权限并不容易(但如果这是剩下的唯一选择,那么会这样做)
UPDATE
@ghoti
./kp是存在的程序,启动程序返回Success. ./NoSuchCommand不存在.从(编辑过的)输出中可以看出,脚本错误地返回Success.
进程完成执行或程序异常终止时无关紧要.通过脚本启动的程序不会以任何方式进行跟踪(因此我们不会将pID存储在任何表中,也不会出现使用deamontools的必要性).
@Etan Reisner
无法启动的程序示例是./NoSuchCommand,它不存在.或者可能是一个无法启动的损坏的程序.
@Vorsprung
调用在后台启动程序的脚本不需要很多时间(并且可以按照我们的期望进行管理).但是睡眠1会随着时间的推移累积而引起问题.
前面提到的#Method3可以很好地限制在ps -eo pID |之前终止的限制进程grep“$PROC_ID”&& {echo“成功”;退出0;可以执行检查.
解决方法 这是一个示例,它将显示一个过程的结果,无论它是否成功启动.#!/bin/bash & #executes a program in background which is provIDed as an argumentpID=$! #stores executed process ID in pIDcount=$(ps -A| grep $pID |wc -l) #check whether process is still runningif [[ $count -eq 0 ]] #if process is already terminated,then there can be two cases,the process executed and stop successfully or it is terminated abnormallythen if wait $pID; then #checks if process executed successfully or not echo "success" else #process terminated abnormally echo "Failed (returned $?)" fielse echo "success" #process is still runningfi#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总结
以上是内存溢出为你收集整理的linux – 检查从同一bash脚本启动的后台进程的运行状态全部内容,希望文章能够帮你解决linux – 检查从同一bash脚本启动的后台进程的运行状态所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)