获取后台进程的退出代码

时间:2009-10-15 04:24:04

标签: linux shell unix process

我有一个从我的主bourne shell脚本调用的命令CMD,它需要永远。

我想修改脚本如下:

  1. 并行运行命令CMD作为后台进程($ CMD&)。
  2. 在主脚本中,每隔几秒钟就有一个循环来监视生成的命令。该循环还向stdout发出一些消息,指示脚本的进度。
  3. 当生成的命令终止时退出循环。
  4. 捕获并报告衍生过程的退出代码。
  5. 有人能指点我完成这个吗?

13 个答案:

答案 0 :(得分:105)

1:在bash中,$!保存已执行的最后一个后台进程的PID。无论如何,这将告诉你要监控什么过程。

4:wait <n>等待PID <n>的进程完成(它将阻塞直到进程完成,所以你可能不想在确定进程完成之前调用它),然后返回已完成进程的退出代码。

2,3:psps | grep " $! "可以告诉您进程是否仍在运行。由您决定如何理解输出并决定它与完成的接近程度。 (ps | grep不是白痴。如果你有时间,你可以想出一个更健壮的方法来判断这个过程是否还在运行。)

这是一个骷髅脚本:

# simulate a long process that will have an identifiable exit code
(sleep 15 ; /bin/false) &
my_pid=$!

while   ps | grep " $my_pid "     # might also need  | grep -v grep  here
do
    echo $my_pid is still in the ps output. Must still be running.
    sleep 3
done

echo Oh, it looks like the process is done.
wait $my_pid
# The variable $? always holds the exit code of the last command to finish.
# Here it holds the exit code of $my_pid, since wait exits with that code. 
my_status=$?
echo The exit status of the process was $my_status

答案 1 :(得分:41)

当我有类似的需求时,这就是我解决它的方式:

# Some function that takes a long time to process
longprocess() {
        # Sleep up to 14 seconds
        sleep $((RANDOM % 15))
        # Randomly exit with 0 or 1
        exit $((RANDOM % 2))
}

pids=""
# Run five concurrent processes
for i in {1..5}; do
        ( longprocess ) &
        # store PID of process
        pids+=" $!"
done

# Wait for all processes to finnish, will take max 14s
for p in $pids; do
        if wait $p; then
                echo "Process $p success"
        else
                echo "Process $p fail"
        fi
done

答案 2 :(得分:7)

#/bin/bash

#pgm to monitor
tail -f /var/log/messages >> /tmp/log&
# background cmd pid
pid=$!
# loop to monitor running background cmd
while :
do
    ps ax | grep $pid | grep -v grep
    ret=$?
    if test "$ret" != "0"
    then
        echo "Monitored pid ended"
        break
    fi
    sleep 5

done

wait $pid
echo $?

答案 3 :(得分:7)

我看到几乎所有答案都使用外部实用程序(主要是ps)来轮询后台进程的状态。有一个更多的unixesh解决方案,捕获SIGCHLD信号。在信号处理程序中,必须检查哪个子进程已停止。可以通过kill -0 <PID>内置(通用)或检查/proc/<PID>目录的存在(特定于Linux)或使用内置的jobs具体来完成。 jobs -l也会报告pid。在这种情况下,输出的第3个字段可以是Stopped | Running | Done | Exit。)。

这是我的例子。

已启动的流程称为loop.sh。它接受-x或数字作为参数。对于-x退出,退出代码为1.对于一个数字,它等待数* 5秒。每5秒打印一次PID。

启动程序进程称为launch.sh

#!/bin/bash

handle_chld() {
    local tmp=()
    for((i=0;i<${#pids[@]};++i)); do
        if [ ! -d /proc/${pids[i]} ]; then
            wait ${pids[i]}
            echo "Stopped ${pids[i]}; exit code: $?"
        else tmp+=(${pids[i]})
        fi
    done
    pids=(${tmp[@]})
}

set -o monitor
trap "handle_chld" CHLD

# Start background processes
./loop.sh 3 &
pids+=($!)
./loop.sh 2 &
pids+=($!)
./loop.sh -x &
pids+=($!)

# Wait until all background processes are stopped
while [ ${#pids[@]} -gt 0 ]; do echo "WAITING FOR: ${pids[@]}"; sleep 2; done
echo STOPPED

有关详细说明,请参阅:Starting a process from bash script failed

答案 4 :(得分:5)

我会略微改变你的做法。如果命令仍处于活动状态并报告消息,则不是每隔几秒检查一次,而是让另一个进程每隔几秒报告该命令仍在运行,然后在命令完成时终止该进程。例如:

#!/bin/sh

cmd() { sleep 5; exit 24; }

cmd &   # Run the long running process
pid=$!  # Record the pid

# Spawn a process that coninually reports that the command is still running
while echo "$(date): $pid is still running"; do sleep 1; done &
echoer=$!

# Set a trap to kill the reporter when the process finishes
trap 'kill $echoer' 0

# Wait for the process to finish
if wait $pid; then
    echo "cmd succeeded"
else
    echo "cmd FAILED!! (returned $?)"
fi

答案 5 :(得分:5)

背景式子进程的pid存储在 $!中。 您可以将所有子进程的pid存储到数组中,例如的 PIDS []

wait [-n] [jobspec or pid …]

等待每个进程ID pid或作业规范jobspec指定的子进程退出并返回等待的最后一个命令的退出状态。如果给出了作业规范,则等待作业中的所有进程。如果未给出参数,则等待所有当前活动的子进程,并且返回状态为零。如果提供了-n选项,则wait等待任何作业终止并返回其退出状态。如果jobspec和pid都没有指定shell的活动子进程,则返回状态为127。

使用等待命令,您可以等待所有子流程完成,同时您可以通过 $?获取每个子流程的退出状态,并将状态存储到状态[] 即可。然后你可以根据状态做一些事情。

我尝试了以下两种解决方案并且运行良好。 solution01 是  更简洁,而 solution02 有点复杂。

solution01

#!/bin/bash

# start 3 child processes concurrently, and store each pid into array PIDS[].
process=(a.sh b.sh c.sh)
for app in ${process[@]}; do
  ./${app} &
  PIDS+=($!)
done

# wait for all processes to finish, and store each process's exit code into array STATUS[].
for pid in ${PIDS[@]}; do
  echo "pid=${pid}"
  wait ${pid}
  STATUS+=($?)
done

# after all processed finish, check their exit codes in STATUS[].
i=0
for st in ${STATUS[@]}; do
  if [[ ${st} -ne 0 ]]; then
    echo "$i failed"
  else
    echo "$i finish"
  fi
  ((i+=1))
done

solution02

#!/bin/bash

# start 3 child processes concurrently, and store each pid into array PIDS[].
i=0
process=(a.sh b.sh c.sh)
for app in ${process[@]}; do
  ./${app} &
  pid=$!
  PIDS[$i]=${pid}
  ((i+=1))
done

# wait for all processes to finish, and store each process's exit code into array STATUS[].
i=0
for pid in ${PIDS[@]}; do
  echo "pid=${pid}"
  wait ${pid}
  STATUS[$i]=$?
  ((i+=1))
done

# after all processed finish, check their exit codes in STATUS[].
i=0
for st in ${STATUS[@]}; do
  if [[ ${st} -ne 0 ]]; then
    echo "$i failed"
  else
    echo "$i finish"
  fi
  ((i+=1))
done

答案 6 :(得分:3)

我们的团队需要使用远程SSH执行的脚本,该脚本在25分钟不活动后超时。这是一个监视循环每秒检查后台进程的解决方案,但每10分钟只打印一次,以抑制不活动超时。

long_running.sh & 
pid=$!

# Wait on a background job completion. Query status every 10 minutes.
declare -i elapsed=0
# `ps -p ${pid}` works on macOS and CentOS. On both OSes `ps ${pid}` works as well.
while ps -p ${pid} >/dev/null; do
  sleep 1
  if ((++elapsed % 600 == 0)); then
    echo "Waiting for the completion of the main script. $((elapsed / 60))m and counting ..."
  fi
done

# Return the exit code of the terminated background process. This works in Bash 4.4 despite what Bash docs say:
# "If neither jobspec nor pid specifies an active child process of the shell, the return status is 127."
wait ${pid}

答案 7 :(得分:2)

一个简单的例子,类似于上面的解决方案。这不需要监视任何进程输出。下一个示例使用tail来跟随输出。

$ echo '#!/bin/bash' > tmp.sh
$ echo 'sleep 30; exit 5' >> tmp.sh
$ chmod +x tmp.sh
$ ./tmp.sh &
[1] 7454
$ pid=$!
$ wait $pid
[1]+  Exit 5                  ./tmp.sh
$ echo $?
5

使用tail跟踪流程输出,并在流程完成后退出。

$ echo '#!/bin/bash' > tmp.sh
$ echo 'i=0; while let "$i < 10"; do sleep 5; echo "$i"; let i=$i+1; done; exit 5;' >> tmp.sh
$ chmod +x tmp.sh
$ ./tmp.sh
0
1
2
^C
$ ./tmp.sh > /tmp/tmp.log 2>&1 &
[1] 7673
$ pid=$!
$ tail -f --pid $pid /tmp/tmp.log
0
1
2
3
4
5
6
7
8
9
[1]+  Exit 5                  ./tmp.sh > /tmp/tmp.log 2>&1
$ wait $pid
$ echo $?
5

答案 8 :(得分:1)

另一种解决方案是通过proc文件系统监控进程(比ps / grep combo更安全);当你启动一个进程时它在/ proc / $ pid中有一个相应的文件夹,所以解决方案可能是

#!/bin/bash
....
doSomething &
local pid=$!
while [ -d /proc/$pid ]; do # While directory exists, the process is running
    doSomethingElse
    ....
else # when directory is removed from /proc, process has ended
    wait $pid
    local exit_status=$?
done
....

现在你可以使用$ exit_status变量了。

答案 9 :(得分:0)

这可能超出了您的问题,但是如果您担心进程运行的时间长度,您可能有兴趣在一段时间后检查运行后台进程的状态。使用pgrep -P $$检查哪些子PID仍在运行是很容易的,但是我想出了以下解决方案来检查那些已经过期的PID的退出状态:

cmd1() { sleep 5; exit 24; }
cmd2() { sleep 10; exit 0; }

pids=()
cmd1 & pids+=("$!")
cmd2 & pids+=("$!")

lasttimeout=0
for timeout in 2 7 11; do
  echo -n "interval-$timeout: "
  sleep $((timeout-lasttimeout))

  # you can only wait on a pid once
  remainingpids=()
  for pid in ${pids[*]}; do
     if ! ps -p $pid >/dev/null ; then
        wait $pid
        echo -n "pid-$pid:exited($?); "
     else
        echo -n "pid-$pid:running; "
        remainingpids+=("$pid")
     fi
  done
  pids=( ${remainingpids[*]} )

  lasttimeout=$timeout
  echo
done

输出:

interval-2: pid-28083:running; pid-28084:running; 
interval-7: pid-28083:exited(24); pid-28084:running; 
interval-11: pid-28084:exited(0); 

注意:如果您愿意,可以将$pids更改为字符串变量而不是数组以简化操作。

答案 10 :(得分:0)

使用此方法,您的脚本不必等待后台进程,您只需要监视临时文件的退出状态。

FUNCmyCmd() { sleep 3;return 6; };

export retFile=$(mktemp); 
FUNCexecAndWait() { FUNCmyCmd;echo $? >$retFile; }; 
FUNCexecAndWait&

现在,您的脚本可以执行任何其他操作,而您只需要继续监视retFile的内容(它还可以包含您想要的任何其他信息,如退出时间)。

PS:顺便说一下,我用bash编码思考

答案 11 :(得分:0)

我的解决方案是使用匿名管道将状态传递到监视循环。没有用于交换状态的临时文件,因此无需清除。如果您不确定后台作业的数量,则中断条件可能为[ -z "$(jobs -p)" ]

#!/bin/bash

exec 3<> <(:)

{ sleep 15 ; echo "sleep/exit $?" >&3 ; } &

while read -u 3 -t 1 -r STAT CODE || STAT="timeout" ; do
    echo "stat: ${STAT}; code: ${CODE}"
    if [ "${STAT}" = "sleep/exit" ] ; then
        break
    fi
done

答案 12 :(得分:0)

怎么样...

# run your stuff
unset PID
for process in one two three four
do
    ( sleep $((RANDOM%20)); echo hello from process $process; exit $((RANDOM%3)); ) & 2>&1
    PID+=($!)
done

# (optional) report on the status of that stuff as it exits
for pid in "${PID[@]}"
do
    ( wait "$pid"; echo "process $pid complemted with exit status $?") &
done

# (optional) while we wait, monitor that stuff
while ps --pid "${PID[*]}" --ppid "${PID[*]}" --format pid,ppid,command,pcpu
do
    sleep 5
done | xargs -i date '+%x %X {}'

# return non-zero if any are non zero
SUCCESS=0
for pid in "${PID[@]}"
do
    wait "$pid" && ((SUCCESS++)) && echo "$pid OK" || echo "$pid returned $?"
done

echo "success for $SUCCESS out of ${#PID} jobs"
exit $(( ${#PID} - SUCCESS ))