Init Script for Daemonizing Non-Forking Processes

5.00 avg. rating (91% score) - 1 vote

Sometimes you have an executable which does not fork to the background, but you need to control it with init scripts, so that it does indeed run in the background. Here’s a pretty generic init script for that. It allows you to configure these:

The script changes directory to the DAEMON_HOMEDIR, runs DAEMON_EXECUTABLE with DAEMON_OPTIONS and saves the new process id to the DAEMON_PIDFILE file. Remember to use different pidfile for every daemon.

The script also checks whether the process is up and running after INIT_SLEEPTIME seconds. If not, it will fail. It will do the check after stopping as well.

All stdout/stderr output is sent to DAEMON_LOGFILE. If you don’t want to log the output, set it to “/dev/null”.

You can control init scripts with this part in the beginning of the file:

Ubuntu/Debian update-rc.d will use those defaults to install it. So after you drop the script under /etc/init.d with the name my_daemon, you can install it using this:

To disable temporarily:

To re-enable:

To remove:

This script does not change running user id from root to something else, so be careful with it.

To start up the daemon, run:

To stop:

Restart:

See status:

The Script

Enough explanation. Here’s the script:

 

Leave a Reply