bash sh脚本nohup排除不完整?

时间:2014-05-19 14:59:18

标签: bash shell ssh command nohup

我有一个问题,请看这段代码。 (j_restart.sh文件)

#!/bin/bash
printf "Killing j-Chat server script... "
nyret=`pkill -f index.php`
printf "OK !\n"
printf "Wait killing instances."
while : ; do
nyret=`netstat -ap | grep :8008 | wc -l`
if [ "$nyret" == "0" ]; then
printf "OK !\n"
break
fi
printf "."
sleep 3
done
echo "Runing j-Chat server script... "
nyret=`nohup php -q /home/jChat/public_html/index.php < /dev/null &`
echo "OK !"
echo "j-Chat Server Working ON !";

ssh return val:

root@server [~]# sh /home/jChat/public_html/j_restart.sh
Killing jChat Server Script... OK !
Wait killing instances................ OK !
Runing jChat Server Script...
nohup: redirecting stderr to stdout
(and waiting not jump next line..)

我按manualy ctrl + c键

^C
root@server [~]#

如何解决这个问题?为什么不完成工作?停止并等待第16行...如何计算下一行17和18 ... ??请帮帮我..

1 个答案:

答案 0 :(得分:0)

以下是复制问题的简单示例:

nyret=`nohup sleep 30 < /dev/null &`
echo "This doesn't run (until sleep exits)"

问题是shell正在等待捕获命令的所有输出。它在后台运行,但它仍然保持管道打开,因此shell等待。

解决方案是不捕获输出,因为您仍然不使用它:

nohup sleep 30 < /dev/null &
echo "This runs fine"