Can a Bourn Shell Script put itself in the background?

Karl Vogel vogelke+unix at
Sat Jun 6 20:44:28 UTC 2009

>> "Barry Byrne" writes:
B> I think the simplest thing would be to write a little wrapper script
B> that calls your other script.
B> #!/bin/sh
B> echo "Stating Daemon Now"
B> /path/to/mainscript &

   You might be better off using daemon to make sure you're detached from
   the controlling terminal.  Other advantages are changing the working
   directory to / in case you need to unmount the filesystem from which
   the original program was run, and properly handling stdin/stdout/stderr.

>> On Sat, 06 Jun 2009 07:10:47 -0500, 
>> Martin McCormick <martin at> said:

M> [...] the output can be redirected to /dev/null or anywhere
M> else when you call the script.  To kill it, do
M>   ps ax | grep SCRIPTNAME | grep -v grep

   If you're looking for an easy way to kill "mainscript", have it store
   its process-id somewhere:
      echo $$ > /var/run/`basename $0`.pid

M> As with all scripts that can start background processes, be careful with
M> loops and such...

   If you're working on each file in a directory, it helps to add
   something like this to the inner loop:
       test -f "stop" && echo stopped at user request && exit 1

   This way, you can just "touch stop" if something goes to hell instead
   of having to grub around in the process table.

Karl Vogel                      I don't speak for the USAF or my company
Therapy is expensive.  Popping bubble wrap is cheap.  You choose.
                                                           --bumper sticker

More information about the freebsd-questions mailing list