≡ Menu

HowTo: Use ps, kill, nice, and killall To Manage processes in FreeBSD and OS X Unix Operating System

I‘m a new Unix system user. How can I manage process on a FreeBSD operating systems?

A process is nothing but an executing program on FreeBSD or Unix-like system. Each process on the system provides the resources needed to run a program such as vim or firefox. Each process has:
  1. A unique process identifier number (PID)
  2. A virtual address space
  3. A security context
  4. Open devices/handles, executable code
  5. Environment variables, priority and more.

In this quick tutorial, I will write about process management on a FreeBSD operating systems.

View running processes in FreeBSD

The ps or process status command is a command that lists process activities. Many people use the top command to interactively list the processes, but for scripting purpose ps is user friendly. The basic syntax is as follows:
$ ps
Sample outputs:

Fig.01: FreeBSD ps command output showing process

Fig.01: FreeBSD ps command output showing process

List process using ps command

Example using flags:
$ ps -auxw

Fig.02 ps command with flags

Fig.02 ps command with flags


Where,

  • a = Display information about other users’ processes as well as your own.
  • u = Display the processes belonging to the specified usernames.
  • x = When displaying processes matched by other options, skip any processes which do not have a controlling terminal. This is the default behavior.
  • w = Use 132 columns to display information, instead of the default which is your window size.

How to list process using top command

Another easiest way to find out what processes are running on FreeBSD is to type top command:
$ top
Sample outputs:

last pid:   762;  load averages:  0.35,  0.30,  0.20                                                                                                 up 0+00:17:04  11:09:01
15 processes:  1 running, 14 sleeping
CPU:  0.0% user,  0.0% nice,  0.0% system,  0.4% interrupt, 99.6% idle
Mem: 13M Active, 16M Inact, 65M Wired, 23M Buf, 3624M Free
Swap: 4071M Total, 4071M Free
 
  PID USERNAME    THR PRI NICE   SIZE    RES STATE    TIME    WCPU COMMAND
  736 ec2-user      1  20    0 17064K  2660K wait     0:00   0.00% sh
  734 ec2-user      1  20    0 25752K  3024K pause    0:00   0.00% screen
  714 ec2-user      1  20    0 86472K  7228K select   0:00   0.00% sshd
  735 ec2-user      1  20    0 25752K  4432K select   0:00   0.00% screen
  712 root          1  20    0 86472K  7228K select   0:00   0.00% sshd
  602 root          1  20    0 24104K  5292K select   0:00   0.00% sendmail
  411 root          1  20    0 14492K  2136K select   0:00   0.00% syslogd
  715 ec2-user      1  20    0 17064K  2668K wait     0:00   0.00% sh
  762 ec2-user      1  20    0 21916K  2816K RUN      0:00   0.00% top
  657 root          1  20    0 16592K  2256K nanslp   0:00   0.00% cron
  627 root          1  20    0 61204K  6556K select   0:00   0.00% sshd
  333 root          1  20    0 13164K  4508K select   0:00   0.00% devd
  294 root          1  52    0 14624K  2236K select   0:00   0.00% dhclient
  605 smmsp         1  52    0 24104K  4952K pause    0:00   0.00% sendmail
  332 _dhcp         1  52    0 14624K  2340K select   0:00   0.00% dhclient

To quit from the top command type (or hit) q and to display help hit h.

Quickly finding the PID of a process on FreeBSD

Use the pgrep command to search the process table on the running system and prints the process IDs (PID) of all processes that match the criteria given on the command line. The syntax is:

pgrep process
pgrep -u user process
pgrep firefox
pgrep -u www-data nginx

A quick way of getting the PID of a process called dhclient is with the pgrep command, type:
$ pgrep dhclient
Sample outputs:

332

How to send processes signals in FreeBSD Unix system

You can send signals to all processes in FreeBSD. For example, SIGTERM can gracefully kill the process.

Send processes signals by PID

The kill command kills process by process identification (PID). Note that the user must have sufficient privileges to kill the process. The basic syntax is as follows:
$ kill <PID>
$ kill -s signalName <PID>

For example:
$ kill -s SIGTERM 454
$ kill -signalName <PID>
$ kill -SIGHUP 6765
$ kill -signalNumber <PID>
$ kill -9 868

To find your PID use pgrep command. It will look like this for example:
$ pgrep firefox
23872
From there you can issue the kill command to kill firefox process:
$ kill 23872
You can also send multiple kills.
$ kill <PID> <PID> <PID>
$ kill 3455 79754 3454

If you want to kill without cleaning up you use the -9 signal:
$ kill -9 <PID>
$ kill -9 5858

List signals

Type the following command:
$ kill -l
Sample output:

Fig.03: List the signal names with the -l option on Unix

Fig.03: List the signal names with the -l option on Unix

Some of the more commonly used signals and meaning

----------+----------------------------------------------
  SIGNAL  |   NAME (Meaning)
----------+----------------------------------------------
     1    |   HUP (hang up)
     2    |   INT (interrupt)
     3    |   QUIT (quit)
     6    |   ABRT (abort)
     9    |   KILL (non-catchable, non-ignorable kill)
     14   |   ALRM (alarm clock)
     15   |   TERM (software termination signal)
--------------------------------------------------------

To kill process by name, use killall command

The basic syntax is as follows:
$ killall process
$ killall firefox
$ killall -SIGNAL process
$ killall -term firefox
$ killall -15 command
$ killall -15 firefox

To kill user’s process:
$ killall -u <user> process
$ killall -u tom firefox

Depending upon your shell you might need different flags or signals. Consult your man pages (man signal). The examples are so you get an idea and not absolute. For example, on my laptop using csh I cannot use the command killall process. I must use at least one option or argument to specify process. Note, there is also pkill command which kills the processes by name, but since it uses pattern matching and not the precise process name, it can be potentially dangerous compared to killall. Here is an example of dangerous pkill command which kills FreeBSD based server or desktop:
$ sudo pkill -v firefox
The -v option reverse the sense of the matching; display processes that do not match the given criteria. See pkill man page for more info.

How to adjust process priorities

The nice command runs process at a low priority. nice assumes the value of 10. The priority is a value range -20 to 20. The default priority is 0, priority 20 is the lowest possible. Negative numbers are expressed by -. You need to be root if you want to change the process prority to higher priorities. Different shells are different so it’s best to consult your nice manual page. I mainly use csh. nice command is more useful on servers or workstation where several processes are demanding more resources than the CPU can provide. Use following commands to set a larger chunk of the CPU time than a lower priority process on a FreeBSD.

nice command usage examples

Execute command date at priority 5 assuming the priority of the shell is 0.
$ nice -n 5 date
This is an example where you need to be superuser or root:
# nice -16 nice --35 date
Execute command date`at priority -19 assuming the priority of the shell is 0. I have never had to use this command because with today’s systems we have such high RAM+CPU and processing power these things aren’t a problem like when first introduced. It’s nice to know nice though just in case.

How to alters the scheduling priority of running processes

The nice command only works when beginning a new process i.e. start of the command. Use the renice command to alters the scheduling priority running processes. The syntax is:
$ renice priority pid
$ renice -n increment pid
$ renice -u user pid

To Change the priority of process ID’s 4242 and 344, and all processes owned by root, enter:
$ sudo renice +1 -u root -p 344 4242

How to see realtime and idle priority of of running processes

A process with a realtime priority is not subject to priority degradation, and will only be preempted by another process of equal or higher realtime priority. A process with an idle priority will run only when no other process is runnable and then only if its idle priority is equal or greater than all other runnable idle priority processes. Priority is an integer between 0 and RTP_PRIO_MAX (usually 31). 0 is the highest priority. 31 is the lowest priority. To see which realtime priority the current process is at, just type the following command:
$ rtprio
Sample outputs:

rtprio: normal priority

To see which realtime priority of process 715:
$ rtprio 715
To change the realtime priority of process 715 to 16, enter:
# rtprio 16 -1423
To run backup.py script while not disturbing other server usage, enter:
# idprio 31 /path/to/my/awesome/backup.py
See rtprio command man page for more info.

This quick tutorial was contributed by Wendy Michele. You can too contribute to nixCraft.

Share this tutorial on:

{ 4 comments… add one }
  • sizonov_stas January 4, 2015, 11:49 pm

    kill+Tab
    killall+Tab
    autocomplit, that work very nice in zsh

  • brnard.mrx January 30, 2015, 5:49 am

    Very useful post i’m sysadmin, cyberciti is like my linux handbook for work!

  • Manu June 9, 2015, 1:21 pm

    Superb tutorial……many many thanks

  • spandana September 3, 2015, 7:14 am

    what is the diff between ps -ef and ps ef ?
    is both are same or is there any difference and which is correct to get count the number of lines of output from a grep
    ps -ef | grep apache | wc -l
    or
    ps ef | grep apache | wc -l

    both are working but count is different .

    Please reply me soon.

Leave a Comment


   Tagged with: , , , ,