: >Is there any way to have a shell script immediately put itself into the
: >background? The equivalent of starting it with a & or hiting ^Z and bg.
: >Thanks.
: If you trust the environment to not contain a
: certain flag variable, you can do this:
: #!/bin/sh
: if test -z "$MYSCRIPT_IS_RUNNING"; then
: MYSCRIPT_IS_RUNNING=1
: export MYSCRIPT_IS_RUNNING
: exit
: fi
: # rest of myscript goes here
Overly complicated.
Two simple solutions:
1) A simple wrapper script that calls the real script (ugly,
uses two files).
2) #!/bin/sh
if [ -t 0 ]
then
echo Backgrounding
exit 0
fi
<rest of script goes here>
This works because '&' als redirects STDIN of the backgrounded
process to /dev/null, preventing the background process from
reading the terminal (which the current foreground process
is ALSO doing - not a nice thing to see).
The "if [ -t 0 ]" tests whether the current STDIN is connected
to a terminal. If so, it is not in the background, so it
unchanged. The parent then exits.
Hope this helps,
Ruurd
--
========================================================================
Ruurd Beerstra, CMG-IT, Netherlands. | #include <stddisclaimer.h>
| what is he doing on my hard disk?