How to keep program running in background in ash shell How to keep program running in background in ash shell shell shell

How to keep program running in background in ash shell


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.