Skip to content
Advertisement

bash: daemonizing by forking process as a new child

I have a bash script which should daemonize itself after being run. My solution looks as follows:

#!/bin/sh -xe
child() {
    echo child
}
child & # fork child
echo parent
kill $$ # kill parent

However, putting the whole script itself inside the function child does not seem the correct thing to do. Unfortunately exec & won’t fork-off the whole process into a backgrounded child.

How can a achieve the desired effect?

Advertisement

Answer

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/bash and /bin/sh which are on most *nix systems are not the same.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement