9.3. Controlling Processes

ps

Will give you a list of the processes running on your system. With no options, ps will list processes that belong to the current user and have a controlling terminal.

Example options include:

  • -aux --- list all running processes (by all users with some information).

  • -a --- list all processes from all users.

  • -u --- list more information including user names, %cpu usage, and %mem usage et cetera.

  • -x --- list processes without controlling terminals.

  • -l --- display different information including UID and nice value.

  • --forest --- this makes it easier to see the process hierarchy, which will give you an indication of how the various processes on your system interrelate (although you should also try pstree).

For example to list all running processes with additional information, simply type:

ps -aux
pstree

Displays the processes in the form of a tree structure (similar to how tree does it for directories).

Use the -p option to show process id's.

Example:

pstree -p

This would list all processes and their id's.

pgrep

This command is useful for finding the process id of a particular process when you know part of its name.

Use the -l option to list the name of the process as well and the -u option to search via a particular user(s).

Normally pgrep will only return the pid number; this way you can use it with other commands.

Examples:

kill $(pgrep mozilla)

This would kill any process name that starts with mozilla. Note that this is the same as using pkill (see below).

If you are unfamiliar with the $( ) part of this command, please refer to Section 6.4.

To list processes id's and names type:

pgrep -l process_name
top

Displays the 'top' (as in CPU usage) processes, provides more detail than ps.

top also provides an updated display, it has many options that make it fully customisable, refer to the manual or info page for details.

kill

To kill processes on your system, you will need their pid's or id's . Use ps or pstree to find out the process id's (pid's), or use jobs to find out id's.

Tipkillall and pkill - kill a process by name
 

pkill and killall can be a lot easier to use than kill. pkill allows you to type part of the name of a process to kill it, while killall requires the full process name. See below for more information.

Examples:

kill pid

Simply kill a process (allow it time to save it's files and exit)

kill %id

Same as above, except it uses an id instead of a pid, you need to use a % (percent) when using an id to kill.

kill -kill pid

Force a process to be killed (won't allow files to be saved or updated); only use when necessary because all data that the program had will be lost.

There are also many other kill options such as kill -HUP (hangup)... refer to the manual/info pages for more information.

killall

Kill a process by it's name, uses names instead of process id's (pid's). Use -v to have killall report whether the kill was successful or not and -i for interactive mode (will prompt you before attempting to kill).

Tippkill - a little like a killall with regular expressions
 

pkill is another command that allows processes to be killed but does so using regular expressions. See below for more information.

For example:

killall -iv mozilla

Would kill anything named “mozilla” and prompt you before each kill and report whether the kill was successful or not. Unfortunately you need to get the name exactly right for killall to work, you would need to use “mozilla-bin” to kill the mozilla browser. If you want something where you don't need to know the exact name try pkill (below).

pkill

pkill is used to kill processes according to an extended regular expression. Use the -u option to kill using a user name(s) and process name (for example to only kill a process of a certain user). pkill can also send specific signals to processes.

For normal usage simply type:

pkill process_name

Note that the “process_name” doesn't have to be an exact match...

Or to kill the “process_name” of only the users “fred” and “anon” type:

pkill -u fred anon process_name
skill

skill is used to send a command/username/tty a particular signal.

skill has a number of options available to ensure correct interpretation (otherwise it just guesses what it is), simply type skill -option(s)

  • -L --- list the various signals that can be sent

  • -u --- specify a username; this is obviously followed by the user name or a space-seperated list of usernames.

  • -p --- process id (followed by the process id)

  • -c --- command name (this is the same as killall)

  • -t --- (tty number)

  • -v --- verbose mode

  • -i --- interactive mode.

skill can be used to stop, continue, or kill processes using the username, command name or process id (or send them any variety of signals you like).

Useful example:

skill -STOP abusive_user_name

The above command will stop all of that users processes, this will cause his screen to freeze until you type:

skill -CONT abusive_user_name

This would tell that all processes may continue as before. Note that this would only work if you are root. Also note you can list more than one user name with the command so it will apply to multiple users.

CTRL-C

The break key, will kill (break, stop) something that's running on your terminal.

jobs

Prints currently running jobs, as in processes you have executed within the shell.

bg

Backgrounds a process. To start a program in the background (so it doesn't take over the terminal) use an “&” (ampersand) sign at the end of the command. You usually use CTRL-Z to suspend something you are currently using. You can simply use bg to resume in the background the last job suspended...

Command syntax:

bg job_number

or

bg job_name
fg

Bring a process to the foreground, so you can interact with it. The process will use your current terminal. Note simply use fg to foreground the last job number suspended...

You can bring jobs to the foreground by name or by number (use jobs to find the number).

Command syntax:

fg job_number

or

fg job_name
nice

Sets the priority for a process. nice -20 is the maximum priority (only administrative users can assign negative priorities), nice 20 is the minimum priority. You must be root to give a process a higher priority, but you can always lower the priority of your own processes...

Example:

nice -20 make

Would execute make and it would run at maximum priority.

renice

Changes the priority of an existing command. You may use the options -u to change the priorities of all processes for a particular user name and -g to change priorities for all processes of a particular group. The default is to change via the process id number.

Example:

renice +20 2222

This would change the priority of process 2222 to +20 (minimum priority).

snice

snice works very similarly to skill, only it changes the priority of the process(es). Its function is similar to that of renice.

To use options (to ensure correct interpretation) you simply type snice -option(s):

  • -u --- specify a username; this is obviously followed by the user name or a space-seperated list of usernames.

  • -p --- process id (followed by the process id)

  • -c --- command name (this is the same as killall)

  • -t --- tty number

  • -v --- verbose mode

  • -i --- interactive mode.

Example:

snice -10 -u root

This would increase the priority of all root's processes.