Unix Daemons in Perl
Unix Daemons in Perl
The word daemon is derived from the Greek word daimon, meaning a "supernatural being" or "spirit", rather than demon, referring to the fallen angels or followers of Satan. Some would insist that Unix is infested with both daemons and demons. In Unix, daemons are typically started by the root process when the operating system is initialized, and run in the background indefinitely. Daemons typically spend most of their time waiting for an event or period when they will perform some task. Examples of common Internet daemons include WU-Ftpd, Apache, BIND, and Sendmail. These particular daemon programs are responsible in part for making the Internet useful, but daemons also serve other purposes that are not as visible to users. In this tutorial, we'll learn how easy it is to turn a Perl script into a daemon process.
First, I want to mention that in many cases, using cron to execute a script at specified times is more appropriate than rolling a daemon from scratch. This is especially true when the script only runs a few times a day or less. In situations where you are waiting for an event that may occur at any moment, a daemon is necessary. Alternatively, if you are performing tasks on a frequent basis, a daemon is more effective because the program stays resident in memory and eliminates the CPU cycles that are involved in starting a new process.
The content of this article assumes you're working in a POSIX compliant Unix environment. The examples will not work in a win32 environment unless you're using a toolkit that implements fork(). Even then, you won't want to use the examples on a win32 system. Instead, I will demonstrate how to create an NT service in the next tutorial.
Before we explore the details of what a daemon is, let's review the characteristics of a Unix process. First, let's take a look at a partial process list:
> ps axj PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 0 1 0 0 ? -1 S 0 0:03 init 1 2 1 1 ? -1 SW 0 0:00 [kflushd] 1 3 1 1 ? -1 SW 0 0:00 [kpiod] 1 4 1 1 ? -1 SW 0 0:00 [kswapd] 1 548 548 548 ? -1 S 0 0:00 httpd 548 557 548 548 ? -1 S 99 0:00 httpd 548 558 548 548 ? -1 S 99 0:00 httpd 548 559 548 548 ? -1 S 99 0:00 httpd 548 560 548 548 ? -1 S 99 0:00 httpd 548 561 548 548 ? -1 S 99 0:00 httpd 548 562 548 548 ? -1 S 99 0:00 httpd 548 563 548 548 ? -1 S 99 0:00 httpd 2450 2452 2452 2452 pts/5 2576 S 501 0:00 zsh
The first column is the parent process id, the process id of the process that created it. The second column contains the process id, which is assigned by the kernel. The next column is the process group id. The fourth column is the session id. A session is a collection of session groups. The next column contains the tty, or controlling terminal, that's related to the process. This is typically a terminal or remote login shell.
For example, if you were to login to a Unix box and executed a command, the kernel assigns a new process, group, and session id whose parent id is the process id of the of the shell that it was executed from.