使用后台进程退出 shell 脚本
我需要以某种方式退出我的 script.sh (带有返回代码 - 将是最好的),它在后台运行一些其他命令和其他脚本。
我尝试通过
nohup myInScript.sh &
在 script.sh 末尾使用来
disown -a[r]
运行命令,并且我也尝试终止它,
kill $$
但脚本只是在最后一个命令之后挂起并且不会退出。 运行命令后如何强制它退出? 请帮忙。
编辑:更具体地说,我通过 ssh 在另一台计算机上远程运行脚本。
I need somehow to exit my script.sh (with return code - would be the best) which runs some other commands and other scripts in the background.
I've tried to run commands via
nohup myInScript.sh &
also I've tried to use at the end of script.sh
disown -a[r]
and also I've tried to kill it
kill $
but the script just hangs after last command and won't quit.
How to force it to exit after running its commands?
please help.
edit: To be more specific, I'm running the script remotely via ssh on the other machine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
上面的解决方案在大多数情况下都有效:
nohup myInScript.sh >some.log 2>&1
但我遇到过一个情况,它没有,不知道为什么。无论如何,at 命令成功了,甚至看起来更优雅:
at now -f my_script.sh
The solution above works most of the time:
nohup myInScript.sh >some.log 2>&1 </dev/null &
But I've had a case where it didn't, not sure why. Anyway the at command did the trick and even looks more classy:
at now -f my_script.sh
从内存中看,如果登录 shell 中任何仍在运行的子级打开了标准(终端)文件句柄,即使登录 shell 完成,该登录 shell 也会保留下来。普通(子进程)shell 似乎不会受到此问题的影响。因此,看看将 nohup 行更改为以下内容是否会产生任何影响。
在 Centos5 上,如果运行
parent.sh
,我不会遇到问题。但如果我运行 ssh localhost Parent.sh ,我就会这样做。在这种情况下,我上面展示的 I/O 重定向解决了问题。From memory a login shell will be kept around even when it finishes if any of its still running children have standard (terminal) file handles open. Normal (sub process) shells do not seem to suffer from this. So see if changing your nohup line to the following makes any difference.
On Centos5 I do not get the problem if I run
parent.sh
. But I do if I runssh localhost parent.sh
. In this case the I/O redirection I showed above solves the problem.