5

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?

Am_I_Helpful
  • 17,636
  • 7
  • 44
  • 68
Marc
  • 2,774
  • 6
  • 39
  • 60
  • 4
    Neither `nohup` nor `screen` are a part of bash. Have you considered installing them? – Ignacio Vazquez-Abrams Apr 17 '15 at 16:12
  • 3
    Steal some C code to create a daemon process, there's tons of it and it'll only take about 30 sloc, have it take your application name as a command line argument and your done. Install your new "init.d" style application on the embedded device, run it when you SSH in and log off. – jiveturkey Apr 17 '15 at 17:16
  • `tmux` should also do the trick. can you install it ? – stdcall Apr 19 '15 at 17:48
  • Does this answer your question? [Getting ssh to execute a command in the background on target machine](https://stackoverflow.com/questions/29142/getting-ssh-to-execute-a-command-in-the-background-on-target-machine) – Lenna Jun 15 '20 at 00:05
  • The only approaches that are specific to bash are ones that use `disown`. Every other option works just as well with ash. – Charles Duffy Jun 15 '20 at 00:41

1 Answers1

4

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.

Lenna
  • 1,040
  • 2
  • 20