Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何写入进程ID并获取下一个到最后一个命令的退出代码_Bash_Exit Code - Fatal编程技术网

Bash 如何写入进程ID并获取下一个到最后一个命令的退出代码

Bash 如何写入进程ID并获取下一个到最后一个命令的退出代码,bash,exit-code,Bash,Exit Code,我想运行一个命令,在命令启动时立即将进程id写入文件,然后获取命令的退出状态。这意味着,虽然必须立即写入进程id,但我只希望在初始命令完成时才显示退出状态 下面的语句将不幸地运行该命令,立即写入进程id,但不会等待命令完成。此外,我将只获取echo命令的退出状态,而不是初始命令的退出状态 命令在我的例子中是rdiff备份 我需要如何修改该语句 <command> & echo $! > "/pid_file" RESULT=$? if [ "$RESULT" -ne "

我想运行一个命令,在命令启动时立即将进程id写入文件,然后获取命令的退出状态。这意味着,虽然必须立即写入进程id,但我只希望在初始命令完成时才显示退出状态

下面的语句将不幸地运行该命令,立即写入进程id,但不会等待命令完成。此外,我将只获取echo命令的退出状态,而不是初始命令的退出状态

命令在我的例子中是rdiff备份

我需要如何修改该语句

<command> & echo $! > "/pid_file"
RESULT=$?
if [ "$RESULT" -ne "0" ]; then
  echo "Finished with errors"
fi
&echo$!>“/pid\u文件”
结果=$?
如果[“$RESULT”-ne“0”];然后
echo“已完成,但有错误”
fi

您需要在后台进程上等待
以获取其退出状态:

_command_for_background_ & echo $! > pid_file
: ... do other things, if any ...
#
# it is better to grab $? on the same line to prevent any
# future modifications inadvertently breaking the strict sequence
#
wait $(< pid_file); child_status=$?
if [[ $child_status != 0 ]]; then
  echo "Finished with errors"
fi
\u命令\u用于\u后台\u和echo$!>pid_文件
: ... 做其他事情,如果有的话。。。
#
#最好抓到美元?在同一条线上,以防止任何
#未来的修改无意中打破了严格的顺序
#
wait$(
似乎和我一起工作。一个问题:为什么称变量为child_status?用于后台的命令\u不是父进程吗?我将其退出代码存储在变量child\u status和echo$中!子进程?这可能只是一个命名问题,但我不想混淆……我想从技术上讲,
command\u for_background
echo
都是孩子,但你感兴趣的是
command\u for_background
。由于后者是您等待的对象,因此将其称为child是有意义的。