How can I get both the process id and the exit cod

2020-02-08 23:47发布

I need a bash script that does the following:

  • Starts a background process with all output directed to a file
  • Writes the process's exit code to a file
  • Returns the process's pid (right away, not when process exits).
  • The script must exit

I can get the pid but not the exit code:

$ executable >>$log 2>&1 &
pid=`jobs -p`

Or, I can capture the exit code but not the pid:

$ executable >>$log;
# blocked on previous line until process exits
echo $0 >>$log;

How can I do all of these at the same time?

标签: bash shell
1条回答
神经病院院长
2楼-- · 2020-02-09 00:08

The pid is in $!, no need to run jobs. And the return status is returned by wait:

$executable >> $log 2>&1 &
pid=$!
wait $!
echo $?  # return status of $executable

EDIT 1

If I understand the additional requirement as stated in a comment, and you want the script to return immediately (without waiting for the command to finish), then it will not be possible to have the initial script write the exit status of the command. But it is easy enough to have an intermediary write the exit status as soon as the child finishes. Something like:

sh -c "$executable"' & echo pid=$! > pidfile; wait $!; echo $? > exit-status' &

should work.

EDIT 2

As pointed out in the comments, that solution has a race condition: the main script terminates before the pidfile is written. The OP solves this by doing a polling sleep loop, which is an abomination and I fear I will have trouble sleeping at night knowing that I may have motivated such a travesty. IMO, the correct thing to do is to wait until the child is done. Since that is unacceptable, here is a solution that blocks on a read until the pid file exists instead of doing the looping sleep:

{ sh -c "$executable > $log 2>&1 &"'
echo $! > pidfile
echo   # Alert parent that the pidfile has been written
wait $!
echo $? > exit-status
' & } | read
查看更多
登录 后发表回答