Managing Processes
Start, stop and restart running processes (FreeBSD)
Posted 08.03.2007 | Updated 08.03.2007 | Contributed by Andy Mallett


Processes are programs which are running on the system. You could argue that an operating system is basically the sum of its processes.

Some processes are required to run the Unix OS while others are 'guest' programs performing some kind of function, such as a web server.

Most guest processes can be started, stopped and restarted and all processes 'belong' to somebody (or sometimes 'nobody'), usually a system user or belong the operating system itself.

Under FreeBSD Unix, processes are usually referred to as Daemons - hence the little horned chappie.
Process management is an important part of system administration and any sysadmin worth his or her salt has a number of process-related commands up their sleeve in case of need. Here the, is a brief introduction to the fine art of process management.

Perhaps the most common is the ps command, and the FreeBSD man page describes ps thus: "The ps utility displays a header line, followed by lines containing information about all of your processes that have controlling terminals.". So not all processes are displayed, but the ones which may need to be managed usually are.

Why would one wish to manage a process? Usually it's to see if a particular service or daemon is running. For instance, to check if the Apache Web Server is running, I would check the ps readout for lines containing httpd, the http daemon. Note most daemons end in the letter 'd'.

The standard ps command doesn't give much away and most administrator supplement it with the switch, ps  -aux. Running the man  ps command tells us a bit more about these commonly-used switches..

-a  Display information about other users' processes as well as your
    own.  This will skip any processes which do not have a controlling
    terminal, unless the -x option is also specified.  This can be
    disabled by setting the security.bsd.see_other_uids sysctl to zero

-u  Display information associated with the following keywords: user,
    pid, %cpu, %mem, vsz, rss, tt, state, start, time, and command.
    The -u option implies the -r option

-x  When displaying processes matched by other options, include
    processes which do not have a controlling terminal.


Because the above command displays a lot of information, much of which immediately scrolls off screen, we modify it a bit more to give one page (screen) at a time..

ps  -aux  |  more

And this dear friends, is probably the most commonly seen modified version of the command. Again there's a lot of information to sift through to find the process you're after, so it's common to grep for the specified daemon thus..

ps  -aux  |  grep  httpd

And straight away I can query the system just for those Apache processes I was interested in before, with the following much-easier-on-the-eye result..

root   375 0.0 0.4 1980 1496 Ss 28Dec06 4:53.17 /usr/local/apache/bin/httpd
nobody 380 0.0 0.4 1992 1544 I  28Dec06 0:00.01 /usr/local/apache/bin/httpd
nobody 381 0.0 0.4 1992 1544 I  28Dec06 0:00.01 /usr/local/apache/bin/httpd
nobody 382 0.0 0.4 1980 1504 I  28Dec06 0:00.00 /usr/local/apache/bin/httpd


Naturally you need to know the name of the process daemon before you can query for its existence in the process list.

Killing Processes

Every process has a unique numeric process identifier (pid) in the second column and this is used to kill the process using the appropriate command structure..

kill  -9  pid

The -9 switch basically means, just do it!. To kill all of the above httpd processes we can chuck them all on the same line..

kill  -9  375  380  381  382

Sometimes you can kill a daemon using its controlling command. As in..

apachectl stop

But again, you need to know which command controls the process.

Starting Processes

This is usually performed by the controlling command, as in..

apachectl start

Restarting Processes

Often a combination of separately killing and then starting a process all over again (with a brand new pid) as seen above. This can also sometimes be achieved if the controlling command supports the relevant switch..

apachectl restart

Type man  ps to find out more about this useful utility.


Other Process Commands

Another useful command is top. Running man  top gives us the following description..

Top displays the top processes on the system and  periodically  updates
    this  information.   If standard output is an intelligent terminal (see
    below) then as many processes as will fit on the  terminal  screen  are
    displayed  by  default.   Otherwise,  a  good  number of them are shown
    (around 20).  Raw cpu percentage is used to  rank  the  processes.   If
    number  is  given,  then  the  top  number  processes will be displayed
    instead of the default.


As top runs 'live' in real time and continually updates itself, you need to press q to quit the top session.

Sometimes starting a process will cause the screen to 'lock' as the process itself has seemingly 'taken over' the login session. To start a process running in the background, thus allowing continued use of the console, we can chuck the & symbol at the end of the command. As in the case of manually starting the mysql daemon..

mysqld_safe  --user=mysql  &    (hit enter twice to get back to the prompt)

See also the nice command for reducing a daemon's priority.


Like the rest of Unix, processes can be fiddled and dabbled with ad infinitum - way beyond the scope of this introductory tome and the passionate reader is urged to sally forth onto the internet in pursuit of further process pontification.