Process and job control

Instructions

Typing kill -l shows you the instructions that kill passes to a process. The following are the most relevant ones for your daily work:

  • SIGHUP: This tells a process to restart immediately after terminating and is often used to tell servers to parse modified configuration files.
  • SIGTERM: This request to terminate allows the process to clean up.
  • SIGKILL: This signal forces a process to terminate come what may. But in some cases, it takes more to get rid of the process. After waiting in vain for a timeout, you have no alternative but to reboot.
  • SIGSTOP: Interrupts the process until you enter SIGCONT to continue.

To send a signal to a process, you can enter either the signal name or number followed by the process ID – for example, kill -19 9201. Also, you can specify multiple process IDs. If you call kill without any parameters but with the PID, it will send the SIGTERM signal to the process.

Seek and Ye Shall Find

To find the right process ID, you can run ps as described previously. The shell command can be combined with other tools, such as grep, in the normal way. For example, you could do this (Listing 3) to find processes with ssh in their names.

Listing 3

grep ssh

01 $ ps aux | grep ssh
02 root    2816 ... 0:00 /usr/sbin/sshd
03 chicken 3992 ... 0:00 ssh -X chicken@asteroid
04 chicken 4249 ... 0:00 ssh chicken@nugget

Besides the SSH server (sshd), the list includes all of your SSH connections. To send the same signal to all of these processes, you would normally list the PIDs in the kill command line, which can be tricky if the list is too long.

The killall gives you a workaround – the tool understands all of the kill signals but expects process names instead of IDs.

The killall -19 ssh command sends all your SSH connections to sleep (SIGSTOP). If you do not specify the signal, killall assumes you mean SIGTERM, just like kill.

Because killall really does remove the processes in one fell swoop, it is a good idea to switch to interactive mode (-i option). For each process, the tool prompts you to decide whether to terminate.

More Detective Work

If you are looking for process IDs, a combination of ps and grep is a good idea, but you can save some typing by running pgrep instead.

To find all processes with ssh in their names, do the following:

$ pgrep ssh
2816
3992
4249

If you need more context, add the -l parameter and pgrep will reveal the names. To discover the full command line, including all arguments, combine -l and -f:

$ pgrep -lf ssh
2816 /usr/sbin/sshd
3992 ssh -X chicken@asteroid
4249 ssh chicken@nugget

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Command Line: Processes

    Innumerable processes may be running on your Linux system. We’ll show you how to halt, continue, or kill tasks, and we’ll examine how to send the remnants of crashed programs to the happy hunting grounds.

  • Shell Scripts in Waiting: the Waitmax Tool

    Linux consultant Matthias Kettner has just released Version 1.0 of his Waitmax Software. It gives programs a specified time to complete after terminating them.

  • Parallel Bash

    You don't need a heavy numeric mystery to benefit from the wonders of parallel processing. This article describes some simple techniques for parallelizing everyday bash scripts.

  • Real-Time Monitoring Tools

    The Top system monitor is a useful aid for identifying system bottlenecks, and Htop, Atop, and Glances extend its possibilities.

  • Command Line – Killing Processes

    Linux offers a variety of tools for shutting down systems and processes. We describe some important commands.

comments powered by Disqus

Direct Download

Read full article as PDF:

089-091_command.pdf  (931.33 kB)

News