I have a bash script which should daemonize itself after being run. My solution looks as follows:
child & # fork child
kill $$ # kill parent
I usually do something like this:
#!/bin/bash if [ -z "$_IS_DAEMON" ]; then _IS_DAEMON=1 /bin/bash $0 "$@" & exit fi echo "I'm a deamon!"
The script effectively restarts itself in the background, while exiting the script started by user.
To recognize the daemonization status, it uses an environment variable (the
$_IS_DAEMON in the example above): if not set, assume started by user; if set, assume started as part of daemonization.
To restart itself, the script simply invokes
$0 "$@": the
$0 is the name of the script as was started by the user, and the
"$@" is the arguments passed to the script, preserved with white-spaces and all (unlike the
$*). I also typically call needed shell explicitly, as to avoid confusion between
/bin/sh which are on most *nix systems are not the same.