Skip site navigation (1)Skip section navigation (2)
Date:      Fri,  5 Jun 2009 18:48:28 -0400 (EDT)
From:      vogelke+unix@pobox.com (Karl Vogel)
To:        freebsd-questions@freebsd.org
Subject:   Re: Can a Bourn Shell Script put itself in the background?
Message-ID:  <20090605224828.18F65BED2@kev.msw.wpafb.af.mil>
In-Reply-To: <20090605190200.GH87456@kokopelli.hydra> (message from Chad Perrin on Fri, 5 Jun 2009 13:02:00 -0600)

next in thread | previous in thread | raw e-mail | index | archive | help
>> On Fri, 5 Jun 2009 13:02:00 -0600, 
>> Chad Perrin <perrin@apotheon.com> 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
     #!/bin/ksh
     # 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+"$@"}
            ;;
     esac
     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.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20090605224828.18F65BED2>