Re: backgrounding a script
MiKe McClain wrote:
> Is there a way for a script to background itself?
Yes. But you gotta be careful, it's very easy to get a run away process
series. The basic idea is that the script has to rerun itself in the
background and exit. The way I do this is to set a flag in the first pass
and *export* it, so the second pass can skip the first pass code. Like
# Got to be careful, we don't want to go into an infinite loop of scripts.
if [ ! "$DoNotBGMe" ]
# Set this so we don't try to rerun this script more than one time.
# Background self. First, get full path to self. This usage of 'type'
# may be specific to the bash shell.
me=$(type -p bgself)
echo Before backgrounding: $$
echo After backgrounding: $$
echo "I'm running!"
# End of script
You can remove the 3 echo commands, they're there for illustration only.
The code you want to have actually run goes after the 'if' statement.
PS: Forgot to mention, you may also not need to do the 'me=$(type...)'
part, I do it so I can verify I'm actually running the script I think I'm
running. Problems of this type can happen if you explicitly set the PATH
being used by script (something you should do if the script is to become a
production level tool, used by many users).