Consider a shell script executed by Sh, not Bash (and I can't change it, and can't use a shebang, it's ignored). The &
operator works, but disown $!
does not and makes Sh complains “disown: not found”.
How do I detach a background process from specifically Sh? I mean, doing from Sh the same as disown
do from Bash.
Best Answer
First let's see what Bash's
disown
command does. From Bash manual:What this means is that it's the shell itself, if it receives
SIGHUP
, forwards it to the background jobs. If the shell exits (andhuponexit
had't been enabled), the background processes—which don't have a controlling terminal—don't getSIGHUP
on terminal closing.So if your concern is to prevent
SIGHUP
to a process launched from a shell wrapper, like e.g.then there's no need in
disown
-like functionality, sincemy-command
will not receiveSIGHUP
unless the shell gets it before exiting.But, there still is a problem if you want to run a child from a script that will continue execution for some time after launching the child, like e.g. here:
The script above will terminate the
sleep 55555
command if the script's controlling terminal closes. Since Bourne shell doesn't have thedisown
builtin that Bash, Ksh and some other shells have, we need another tool. That tool isnohup(1)
. The above script, in which we aren't interested in thestdout
andstderr
of the child process, can be modified to the following to avoidsleep
gettingSIGHUP
:The redirection to
/dev/null
is to avoid getting thenohup.out
file in current directory. Without the redirection, this file will contain the output of the nohupped process.