MicroBSD Handbook

Prev Next

3.6 Daemons, Signals, and Killing Processes

When you run an editor it is easy to control the editor, tell it to load files, and so on. You can do this because the editor provides facilities to do so, and because the editor is attached to a terminal. Some programs are not designed to be run with continuous user input, and so they disconnect from the terminal at the first opportunity. For example, a web server spends all day responding to web requests, it normally does not need any input from you. Programs that transport email from site to site are another example of this class of application.

We call these programs daemons. Daemons were characters in Greek mythology; neither good or evil, they were little attendant spirits that, by and large, did useful things for mankind. Much like the web servers and mail servers of today do useful things. This is why the BSD mascot has, for a long time, been the cheerful looking daemon with sneakers and a pitchfork.

There is a convention to name programs that normally run as daemons with a trailing ``d''. BIND is the Berkeley Internet Name Daemon and the actual program that executes is called named, the Apache web server program is called httpd, the ftp daemon is ftpd and so on. This is a convention, not a hard and fast rule; for example, the main mail daemon for the Sendmail application is called sendmail, and not maild, as you might imagine.

Sometimes you will need to communicate with a daemon process. These communications are called signals, and you can communicate with daemons (or with any running process) by sending it a signal. There are a number of different signals that you can send, some of them have a specific meaning, others are interpreted by the application, and the application's documentation will tell you how that application interprets signals. You can only send a signal to a process that you own. If you send a signal to someone else's process with kill or permission will be denied. The exception to this is the root user, who can send signals to everyone's processes.

MicroBSD will also send applications signals in some cases. If an application is badly written, and tries to access memory that it is not supposed to, MicroBSD sends the process the Segmentation Violation signal (SIGSEGV). If an application has used the alarm system call to be alerted after a period of time has elapsed then it will be sent the Alarm signal (SIGALRM), and so on.

Two signals can be used to stop a process, SIGTERM and SIGKILL. SIGTERM (SIGNAL TERMINATION) is the polite way to kill a process; the process can catch the signal, realize that you want it to shut down, close any log files it may have open, and generally finish whatever it is doing at the time before shutting down. In some cases a process may even ignore SIGTERM if it is in the middle of some task that can not be interrupted.

SIGKILL (SIGNAL KILL) can not be ignored by a process. This is the ``I do not care what you are doing, stop right now'' signal. If you send SIGKILL to a process then MicroBSD will stop that process immediately.

The other signals you might want to use are SIGHUP, SIGUSR1, and SIGUSR2. These are general purpose signals, and different applications will do different things when they are sent.

Suppose that you have changed your web server's configuration file, you would like to tell the web server to re-read its configuration. You could stop and restart httpd, but this would result in a brief outage period on your web server, which may be undesirable. Most daemons are written to respond to the SIGHUP signal by re-reading their configuration file. So instead of killing and restarting httpd you would send it the SIGHUP signal. Because there is no standard way to respond to these signals, different daemons will have different behavior, so be sure and read the documentation for the daemon in question.

Signals are sent using the kill command, as this example shows.

Sending a Signal to a Process

This example shows how to send a signal to inetd. The inetd configuration file is /etc/inetd.conf, and inetd will re-read this configuration file when it is sent SIGHUP.

  • Find the process ID of the process you want to send the signal to. Do this using ps and grep. The grep command is used to search through output, looking for the string you specify. This command is run as a normal user, and inetd is run as root, so the ax options must be given to ps.

        # ps -ax | grep inetd
        165 ??  Is      0:00.01 (inetd)
      
      

    So the inetd PID is 165. In some cases the grep inetd command might also occur in this output. This is because of the way ps has to find the list of running processes.


  • Use kill to send the signal.

    	# /bin/kill -s HUP 165
      
  • In common most with Unix commands, kill will not print any output if it is successful. If you send a signal to a process that you do not own then you will see ``PID: Operation not permitted''. If you mistype the PID you will either send the signal to the wrong process, which could be bad, or, if you are lucky, you will have sent the signal to a PID that is not currently in use, and you will see ``PID: No such process''.

    WARNING:Killing random process on the system can be bad idea!


    Prev Home Next
    Processes Shells

    This, and other documents, can be downloaded from MicroBSD.

    For questions about MicroBSD, read the documentation before contacting <MicroBSD Support>.
    Copyright © 1995-2003 by The FreeBSD Documentation Project, OpenBSD FAQ Copyright © 1998-2003 OpenBSD, Modified for MicroBSD