问题
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?
回答1:
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. Orphans only die when the init process dies.
The difference between putting subshell in background vs putting command in background is that subshells have different process states
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.
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.
来源:https://stackoverflow.com/questions/29704358/how-to-keep-program-running-in-background-in-ash-shell