Notice:

This page has been converted from a dynamic Wordpress article to a static HTML document. As a result, some content may missing or not rendered correctly.

Create A Daemon Out Of Any Program ~ Fri, 04 Nov 2011 17:12:40 +0000

I find myself having to write a script that does this sort of thing every time I have a need. I finally got fed up enough with that to write a generic script that can be used with any interactive program. The script can be found on my BitBucket site under the Scripts repository; the script is named screen_daemon.sh.

The script, as should be obvious, relies on the screen utility to manage the "daemon." Screen specifically supports this type of thing with the "-Dm" switch combination. This switch combination starts screen in detached mode but doesn't "fork a new process" (man screen). This means that screen starts whatever program you pass in and quits as soon as that program terminates. Thus, you can safely assume that the process id (pid) of the screen session is the pid of your daemon. On lines 82 - 84 of screen_daemon.sh I provide an example that takes advantage of this fact for programs that don't write their own pid file.

If you create a daemon using this script, you can add a cronjob that runs your daemon script at regular interval, say every five minutes, to keep the daemon running. If the daemon is already running, the script will simply exit. If the script determines that an instance is not running, or has crashed in some way, it will start a new instance.

Uncategorized