I am trying to write a Linux shell script (preferably bash), supposedly called detach.sh , to safely disconnect programs from the terminal, such that:
Call: ./detach.sh prog [arg1 arg2 ...] .
Is exec -able, for example. running this in its shell:
exec ./detach.sh prog [arg1 arg2 ...]
With proper quoting (mostly handling arguments containing spaces).
Discards exits (since they are not needed).
Does not use screen , tmux , etc. (for the same reason with 4, plus there is no need for an additional babysitting process).
It uses (reasonably) portable commands and programs, and there are no such things as start-stop-daemon , which is quite specific to the distribution.
I thought of several ways (the shebang lines #!/bin/bash neglected for brevity):
nohup :
nohup "$@" >& /dev/null &
disown :
"$@" >& /dev/null & disown
setsid :
setsid "$@" >& /dev/null &
Using a subshell:
("$@" >& /dev/null &)
nohup / setsid in combination with a subshell:
# Or alternatively: # (nohup "$@" >& /dev/null &) (setsid "$@" >& /dev/null &)
When using gedit as a test program (substituting the "$@" ), condition 1 can be satisfied by all the above methods, but condition 2 cannot be satisfied by anything.
However, if an arbitrary program (but not a built-in shell) is added to script 5, all the conditions seem to be satisfied (at least for me in the case of gedit ). For example:
(setsid "$@" >& /dev/null &)
Anyone who has an idea of explaining the above phenomena and how to properly fulfill the requirements?
EDIT:
With condition 2, I mean that the program must be disconnected from the terminal, but it works as usual otherwise. For example, in the case of gedit condition is not met if gedit just exits immediately after the script process completes.
scripting bash shell
Casper Ti. Vector Apr 20 2018-12-12T00: 00Z
source share