Skip to content
Advertisement

how to daemonize a script

I am trying to use daemon on Ubuntu, but I am not sure how to use it even after reading the man page.

I have the following testing script foo.sh

#!/bin/bash
while true; do
    echo 'hi' >> ~/hihihi
    sleep 10
done

Then I tried this command but nothing happened:

daemon --name="foo" -b ~/daemon.out -l ~/daemon.err -v -- foo.sh

The file hihihi was not updated, and I found this in the errlog:

20161221 12:12:36 foo: client (pid 176193) exited with 1 status

How could I use the daemon command properly?

Advertisement

Answer

AFAIK, most daemon or deamonize programs change the current dir to root as part of the daemonization process. That means that you must give the full path of the command:

daemon --name="foo" -b ~/daemon.out -l ~/daemon.err -v -- /path/to/foo.sh

If it still did not work, you could try to specify a shell:

daemon --name="foo" -b ~/daemon.out -l ~/daemon.err -v -- /bin/bash -c /path/to/foo.sh
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement