Shell 脚本:获取 Solaris 中失败的后台子进程的退出状态
我写了一个 POC。这段代码在 Linux 中运行良好,但在 Solaris 中不行。我使用的是 Solaris 10,
enter code here
#!/bin/sh
echo inside parent
echo executing child in the background
./remove deepti & # executing dummy command to make sure that background process fails
childpid=$!
i=0
while [ `ps -p $childpid >/dev/null; echo $?` = 0 ]; do
sleep 5
i=`expr $i + 1`
if [ $i -gt 3 ]; then
echo wait exceeded
ps -p $childpid >/dev/null
exit $?
fi
done
wait $childpid
exit $?
我希望 Wait 能够返回后台命令的退出状态。退出状态应该是 127 。但是我得到的退出状态为 0。
I have written a POC .This code works fine in Linux but not in Solaris. I am using Solaris 10
enter code here
#!/bin/sh
echo inside parent
echo executing child in the background
./remove deepti & # executing dummy command to make sure that background process fails
childpid=$!
i=0
while [ `ps -p $childpid >/dev/null; echo $?` = 0 ]; do
sleep 5
i=`expr $i + 1`
if [ $i -gt 3 ]; then
echo wait exceeded
ps -p $childpid >/dev/null
exit $?
fi
done
wait $childpid
exit $?
I expect that Wait shall return me the exit status of background command . exit status should be 127 . However I get exit status as 0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是一个错误,而是 /bin/sh 在 Solaris 上的预期和记录的行为。引自 wait(1) 手册页< /a>:
如果 pid 不是活动进程 ID,则等待实用程序将立即返回,并且返回代码将为 0。
旧版 Bourne shell 文档未指定等待内容当用已经死的进程调用时应该这样做,因此结果是未定义的行为。 1998 年修复了该文档以澄清这一点。 ksh 是获取预期返回状态的推荐方法: http://bugs.opensolaris.org/bugdatabase/view_bug.do?bug_id=4068875
对于 Solaris 10 及更早版本,/bin/sh 是一种旧版脚本语言,不应与 new 一起使用代码。对于 POSIX 脚本,您宁愿使用 /bin/ksh 或 /usr/xpg4/bin/sh 来代替。 Solaris 11 Express 提供了 POSIX /bin/sh,因此此类问题不会再发生。
It is not a bug but the expected and documented behavior of /bin/sh on Solaris. Quoted from wait(1) manual page:
If pid is not an active process ID, the wait utility will return immediately and the return code will be 0.
Older Bourne shell documentation was not specifying what wait should do when invoked with already dead processes so the result was undefined behavior. The documentation was fixed to clarify that point in 1998. ksh was the recommended way to get the expected return status: http://bugs.opensolaris.org/bugdatabase/view_bug.do?bug_id=4068875
With Solaris 10 and older, /bin/sh is a legacy scripting language that shouldn't be used with new code. You'd rather use /bin/ksh or /usr/xpg4/bin/sh instead for POSIX scripts. Solaris 11 Express provides a POSIX /bin/sh so this kind of issues would no more happen.