How to keep program running in background in ash shell
Asked Answered
S

1

9

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?

Seidule answered 17/4, 2015 at 16:11 Comment(5)
Neither nohup nor screen are a part of bash. Have you considered installing them?Heal
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.Syncom
tmux should also do the trick. can you install it ?Overcoat
Does this answer your question? Getting ssh to execute a command in the background on target machinePelvic
The only approaches that are specific to bash are ones that use disown. Every other option works just as well with ash.Doublecheck
P
8

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.

Pelvic answered 24/3, 2020 at 3:27 Comment(2)
Wow, that actually works, thank you! Care to explain how?Dortch
@Paul Hope that helps!Pelvic

© 2022 - 2024 — McMap. All rights reserved.