所以我想validation特定命令的退出码
runuser -s /bin/bash @user_name -c $command > /dev/null 2>&1 &
我怎么才能find命令runuser -s /bin/bash @user_name -c $command
是否正确执行? 我试过使用$?
但它不工作,因为它总是0(redirect的结果是0)
我怎样才能find该命令的退出码?
由于任务的背景(通过&
),退出代码不可用,而不是重定向。
您应该使用wait来获取后台任务的退出代码。
等待命令停止脚本执行,直到在后台运行的所有作业都已终止,或者直到作为选项指定的作业编号或进程标识终止。 它返回等待命令的退出状态。
你可以使用$!
在bash中获取最后一个后台进程的pid。 那么你可以wait pid
。 这将等到pid过程完成。
所以对于你的脚本你可以做这样的事情:
runuser -s /bin/bash @user_name -c $command > /dev/null 2>&1 & myPid=$! wait $myPid status=$? echo "Exit status of the process with pid: $myPid is $status"