与后台进程退出shell脚本 [英] exiting shell script with background processes

查看:329
本文介绍了与后台进程退出shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以某种方式退出我script.sh(与返回code - 将是最好的),它在后台运行一些其他的命令和其他脚本

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.

我试图运行命令通过

nohup myInScript.sh &

我也试过在script.sh年底

also I've tried to use at the end of script.sh

disown -a[r]

和还我试图杀死它。

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.

编辑:更具体,我通过另一台机器上的SSH远程运行脚本

edit: To be more specific, I'm running the script remotely via ssh on the other machine.

推荐答案

从记忆即使当它完成,如果任何仍在运行的孩子有标准(终端)的文件句柄打开一个登录shell将围绕保持。正常(子流程)弹似乎还没有从这个受苦。所以,看是否改变你的nohup的线下有什么差别。

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.

nohup myInScript.sh >some.log 2>&1 </dev/null &

在Centos5我不,如果我跑得到问题 parent.sh 。但是,如果我跑 SSH本地主机parent.sh 。在这种情况下,I / O重定向我上面显示出解决问题。

On Centos5 I do not get the problem if I run parent.sh. But I do if I run ssh localhost parent.sh. In this case the I/O redirection I showed above solves the problem.

这篇关于与后台进程退出shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆