help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] On writing a shell script to safely background programs


From: Casper Ti. Vector
Subject: [Help-bash] On writing a shell script to safely background programs
Date: Thu, 19 Apr 2012 15:27:57 +0800
User-agent: Mutt/1.5.21 (2010-09-15)

Hello help-bash, I'm trying to write a shell script, supposedly named
`bg.sh', to safely put programs to background, such that:

1. Invocation:
    ./bg.sh prog [arg1 arg2 ...]
2. Is `exec'-able, eg. by running this in your shell:
    exec ./bg.sh prog [arg1 arg2 ...]
3. Proper quoting (mainly handling of arguments containing whitespaces).
4. Discards the outputs (since they are unneeded).
5. Does not use screen, tmux, etc. (same reason with 4, plus no need for
   an extra babysitting process).
6. Uses (reasonably) portable commands and programs, and no things like
   `start-stop-daemon' which is quite distro-specific.

I have thought of several ways(shebang lines `#!/bin/bash' neglected to
save disk space for the mail list):

1. `nohup':
| nohup "$@" >& /dev/null &
2. `disown':
| "$@" >& /dev/null &
| disown
3. `setsid':
| setsid "$@" >& /dev/null &
4. Using a subshell:
| ("$@" >& /dev/null &)
5. `nohup'/`setsid' combined with subshell:
| # Or alternatively:
| # (nohup "$@" >& /dev/null &)
| (setsid "$@" >& /dev/null &)

When using `gedit' as the test program (substituting the `"$@"' part),
condition 1 can be satisfied with all the above methods, but condition 2
can be satisfied with none.

However, if an arbitrary program (but not a shell builtin) is appended
to script 5, all the conditions seem to be satisfied (at least for the
`gedit' test). For example:
| (setsid "$@" >& /dev/null &)
| # Not just `true' because it is also a shell builtin.
| /bin/true

Anyone with an idea about an explanation of the above phenomenons and
how to correctly implement the requirements? Any suggestion is welcome.
Thanks :)

-- 
    Using GPG/PGP? Please get my current public key (ID: 0xAEF6A134,
valid from 2010 to 2013) from a key server.

Attachment: signature.asc
Description: Digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]