以下脚本显示了我在不同的系统(Fedora,CentOS和RHEL)上运行的不一致行为:
#!/bin/sh -p
set -x
SCRIPT=`basename $0`
case "$1" in
start)
./$SCRIPT start_internal &
;;
start_internal)
eval "nohup sleep 5 > test.log 2>&1 &"
pid=$!
echo "PROC is started (with pid $pid)..."
# Trap signals 9 and 15 and pass on to child process
trap 'kill $pid' 2 3 15
wait $!
echo "I'm done!"
exit 0
;;
esac
exit 0
这是我从一个更大的 init.d 脚本中提取的测试脚本,我应该对其进行调试。因此,它通常使用scriptname start
手动或某些 init 系统来运行。
行为一
在我的 Centos 7.3 和 Fedora 25 系统上,脚本立即退出并显示以下输出:
$ ./test.sh start
++ basename ./test.sh
+ SCRIPT=test.sh
+ case "$1" in
+ ./test.sh start
++ basename ./test.sh
+ SCRIPT=test.sh
+ case "$1" in
+ exit 0
+ ./test.sh start_internal
+ exit 0
++ basename ./test.sh
+ SCRIPT=test.sh
+ case "$1" in
+ eval 'nohup sleep 5 > test.log 2>&1 &'
+ pid=25969
+ echo 'PROC is started (with pid 25969)...'
PROC is started (with pid 25969)...
+ trap 'kill $pid' 2 3 15
+ wait 25969
++ nohup sleep 5
$
然后在睡眠时间结束后控制台打印出以下内容:
+ echo 'I'\''m done!'
I'm done!
+ exit 0
$
行为二
在运行 Centos 6.8 和 RHEL 7.2 的另外两个系统上,它只是挂在该行之后++ nohup sleep 5
并等待睡眠时间到期或用户按下回车键。
$ ./test.sh start
++ basename ./test.sh
+ SCRIPT=test.sh
+ case "$1" in
+ ./test.sh start
++ basename ./test.sh
+ SCRIPT=test.sh
+ case "$1" in
+ exit 0
+ ./test.sh start_internal
+ exit 0
$ ++ basename ./test.sh
+ SCRIPT=test.sh
+ case "$1" in
+ eval 'nohup sleep 5 > test.log 2>&1 &'
+ pid=7304
+ echo 'PROC is started (with pid 7304)...'
++ nohup sleep 5
PROC is started (with pid 7304)...
+ trap 'kill $pid' 2 3 15
+ wait 7304
我相信行为一是预期的行为,而行为二是与操作系统差异相关的错误,我现在应该找到它。
有什么想法我应该从哪里开始调查?