bash nohup 无法正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40614243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
nohup does not work properly
提问by nikosdi
I have a very simple bash script. I want this bash script to continue running after I log out from the terminal since it is monitoring some services. However, I have the problem that as soon as I exit the terminal the process is terminated.
我有一个非常简单的 bash 脚本。我希望这个 bash 脚本在我从终端注销后继续运行,因为它正在监视一些服务。但是,我遇到的问题是,一旦我退出终端,进程就会终止。
I run the process as:
我运行这个过程:
nohup ./test.sh > test.log &
I check the process using:
我使用以下方法检查过程:
ps -aux | grep test.sh
When I run the process I check that the script is running. However, when I reconnect the script is not running anymore.
当我运行该过程时,我会检查脚本是否正在运行。但是,当我重新连接时,脚本不再运行。
The content of the test.sh file is the following:
test.sh文件内容如下:
#!/bin/bash
while :
do
echo `date`": ****** Scheduled Test *****"
result1=$(wget "127.0.0.1" -q -O -)
result2=$(wget "127.0.0.1" -q -O -)
echo ": $result1"
echo ": $result2"
if [[ $result1 == *"Running OK"* ]] && [[ $result2 == *"Running OK"* ]];
then
echo "***** Running OK ***** :)"
sleep 60
continue
fi
echo "@@@@@ Not Running @@@@@"
echo "-----> Killing JARS"
kill -9 `lsof -i:4445 | cut -d' ' -f5`
kill -9 `lsof -i:4423 | cut -d' ' -f5`
./runsomejar.sh > jar1.log &
echo "-----> Restarting jar1"
sleep 60
echo "-----> Restarting jar2"
./runsomejar.sh > jar2.log &
sleep 180
done
回答by Eric Duminil
You could use screen
:
你可以使用screen
:
screen -L ./test.sh
You can use Ctrl-A
and d
to detach the screen.
您可以使用 Ctrl-A
和d
来分离屏幕。
You can log out without killing test.sh
不杀就可以注销 test.sh
You can log back, and type
您可以重新登录,然后键入
screen -r
to come back to your script.
回到你的剧本。
Stdout will be logged to screenlog.0
标准输出将被记录到 screenlog.0
回答by srp
I think you might have already figured out the root cause and a working solution.
我想您可能已经找到了根本原因和可行的解决方案。
But I am writing this answer so that it will be helpful to someone else.
但我正在写这个答案,以便对其他人有所帮助。
Thismight be the root cause.
这可能是根本原因。
Still there is no guarantee in consistency of nohup
(mentioned at the end of given answer).
仍然不能保证nohup
(在给定答案的末尾提到)的一致性。
Other commands to do the same thing.
其他命令做同样的事情。
spawning subshells with parenthesis.
(./test.sh > test.log &)
disown in conjunction with the backgrounding ampersand.
./test.sh > test.log & disown
生成带括号的子shell。
(./test.sh > test.log &)
与背景符号一起拒绝。
./test.sh > test.log & disown