Articles on websites & etc.

§ Home > Index > Linux and FreeBSD

Starting Daemons from Cron

This article describes how to start daemon (background) processes from the cron scheduler on FreeBSD.

Suppose you have a background process that runs all the time. These are typically called services or daemons. Such a process can be started when the server boots up by specifying @reboot as the scheduled time in the crontab entry. The alternative to using cron is to write an rc.d script, which would be automatically executed by the system on startup. An rc.d script gives you much more flexibility, but is also more complicated.

The crontab entry to start your daemon may look something like this:

@reboot /usr/bin/perl /home/me/

The problem with this method is that cron will spawn three processes to maintain the daemon, as shown below.

1 cron: running job (cron)
2 /bin/sh -c /usr/bin/perl
3 /usr/bin/perl

You can find the list of processes by running ps axl (FreeBSD syntax). Note, full paths are omitted for brevity.

Placing "&" at the end of the command causes to run in the background. However, the other two processes still remain, and /bin/sh process becomes a zombie.

In order to avoid the zombie, use the shell built-in command, exec to run the daemon. This will cause the daemon process to replace the shell process /bin/sh. The crontab would look like this:

@reboot exec /usr/bin/perl

Cron will now create two processes, like this:

1 piperd I     cron: running job (cron)
2 sbwait Ss    /usr/bin/perl

The first process is cron capturing the output of the daemon. If you do not want the extra process, and you do not need cron to capture the output then you can do this:

@reboot exec /usr/bin/perl >> mydaemon.log 2>&1 &

The ">> mydaemon.log" redirects stdout and appends it to the file, while the "2>&1" causes stderr to be redirected to stdout. Thus, all output streams of the daemon are appended to the file. Finally, the trailing "&" makes the process run in the background. This results in a clean process table with only the daemon active. Without the ampersand, cron still waits for output from the command, even though all output is redirected.


Limiting the number of processes can be important on a virtual private server, where the number of concurrent processes may be limited. For example, on a Verio VPS v2 Basic, the limit is 64, and on the v2 1000, it is 75 (as of Mar. 18, 2006).

If has execute permission and begins with #!/usr/bin/perl, you won't need to write /usr/bin/perl on the command line. However, if you run these scripts on Windows as well as FreeBSD, then specifying the interpreter (perl) on the command line instead of in the script makes the execution method consistent across platforms.

Sample Daemon

For testing, save the code below to, and you can use it as a test daemon.


Have you heard of the new, free Automated Feeds offered by Google Merchant Center? Learn more in Aten Software's latest blog post comparing them to traditional data feed files.
Created 2006-03-18, Last Modified 2011-07-24, © Shailesh N. Humbad
Disclaimer: This content is provided as-is. The information may be incorrect.