简体   繁体   English

如何在 ash shell 中保持程序在后台运行

[英]How to keep program running in background in ash shell

I need to SSH to an embedded device, launch a background program, then disconnect and keep the background process running.我需要通过 SSH 连接到嵌入式设备,启动后台程序,然后断开连接并保持后台进程运行。 The problem is that the embedded device is using the ash shell (NOT bash or anything else), so nohup and screen are NOT available.问题是嵌入式设备正在使用 ash shell(不是 bash 或其他任何东西),所以 nohup 和 screen 不可用。 I have not been able to find any way of disconnecting the process in ash.我还没有找到任何方法来断开 ash 中的进程。 Is there any way of doing this in ash?有没有办法在灰烬中做到这一点?

An alternative to:替代方案:

nohup command &

Using clever parentheses:使用巧妙的括号:

(( command & ) & )

And also if you want to drop stdin/stdout:而且如果你想删除标准输入/标准输出:

(( command 0<&- &>/dev/null &) &)

Explanation解释

TLDR : We made a subshell start a subshell to execute a command, thus starting an orphan process. TLDR :我们让一个子shell启动一个子shell来执行一个命令,从而启动一个孤立进程。 Orphans only die when the init process dies.孤儿只会在 init 进程死亡时死亡。

The difference between putting subshell in background vs putting command in background is that subshells have different process states 将子shell置于后台与将命令置于后台的区别在于子shell具有不同的进程状态

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.当您注销 SSH 会话或关闭任何sh shell 会话时,会向该 shell 的所有子进程发送一个SIGHUP信号。 What we did here is we started a subshell to start a subshell, thus disowning the started process.我们在这里所做的是我们启动了一个子shell来启动一个子shell,从而否认启动的进程。 We now have an orphan process .我们现在有一个孤儿进程

This orphaned process no longer has a PPID (parent process ID) that identifies with our SSH session.这个孤立的进程不再有一个 PPID(父进程 ID)来标识我们的 SSH 会话。 So when we logout of the SSH session and a SIGHUP is sent to all of our child processes, it never hits the orphan.因此,当我们注销 SSH 会话并向所有子进程发送SIGHUP ,它永远不会命中孤儿。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM