如何保持程序在ash shell中在后台运行 [英] How to keep program running in background in ash shell
问题描述
我需要SSH到嵌入式设备,启动后台程序,然后断开连接并保持后台进程运行.问题在于嵌入式设备正在使用ash shell(不能使用bash或其他工具),因此nohup和screen不可用.我还没有找到任何断开ash中进程的方法.有什么办法用灰做吗?
I need to SSH to an embedded device, launch a background program, then disconnect and keep the background process running. The problem is that the embedded device is using the ash shell (NOT bash or anything else), so nohup and screen are NOT available. I have not been able to find any way of disconnecting the process in ash. Is there any way of doing this in ash?
推荐答案
替代方案:
nohup command &
使用巧妙的括号:
(( command & ) & )
如果要删除stdin/stdout:
And also if you want to drop stdin/stdout:
(( command 0<&- &>/dev/null &) &)
说明
TLDR :我们制作了一个子外壳,启动了一个子外壳来执行命令,从而启动了一个孤立进程.孤儿只有在初始化过程终止后才会死亡.
Explanation
TLDR: We made a subshell start a subshell to execute a command, thus starting an orphan process. Orphans only die when the init process dies.
将subshell放入背景与放置之间的区别后台命令是子外壳具有不同的进程状态
注销SSH会话或关闭任何sh
Shell会话时,会向该Shell的所有子进程发送SIGHUP
信号.我们在这里所做的是,我们启动了一个子外壳来启动一个子外壳,因此 disowning 已启动的过程.现在,我们有一个孤立进程.
When you log out of an SSH session or close any sh
shell session a SIGHUP
signal is sent to all of the child processes of that shell. What we did here is we started a subshell to start a subshell, thus disowning the started process. We now have an orphan process.
此孤立进程不再具有与我们的SSH会话标识的PPID(父进程ID).因此,当我们注销SSH会话并向所有子进程发送SIGHUP
时,它永远不会碰到孤儿.
This orphaned process no longer has a PPID (parent process ID) that identifies with our SSH session. So when we logout of the SSH session and a SIGHUP
is sent to all of our child processes, it never hits the orphan.
这篇关于如何保持程序在ash shell中在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!