Can a Bourn Shell Script put itself in the background?

Karl Vogel vogelke+unix at
Fri Jun 5 22:49:10 UTC 2009

>> On Fri, 5 Jun 2009 13:02:00 -0600, 
>> Chad Perrin <perrin at> said:

C> I got the impression this question was about a script backgrounding itself,
C> though -- possibly creating a daemon using bash.

   Same here.  This seems a bit slimy, but it works (assuming you don't
   already have an environment variable called DAEMON):

     me% cat doit
     # script to daemonize itself.

     PATH=/usr/local/bin:/bin:/usr/bin:/usr/sbin; export PATH
     umask 022
     env | grep 'DAEMON=yes' > /dev/null

     case $? in
         0) logger -t test "$$ is a daemon, args $@"
         1) echo "$$ not a daemon, args $@"
            DAEMON=yes daemon $0 ${1+"$@"}
     exit 0

     me% ./doit a b c
     18131 not a daemon, args a b c

     me% tail -1 /var/log/syslog
     Jun  5 18:41:54 host test: 18135 is a daemon, args a b c

Karl Vogel                      I don't speak for the USAF or my company
Gingko Viagra: to help you remember what the f*** you're doing.

More information about the freebsd-questions mailing list