Help Docs Server Administration Linux Server Administration Managing Processes with the kill command

Managing Processes with the kill command

The kill command helps manage server processes by sending signals. It's crucial for stopping unresponsive programs, but use caution to avoid disrupting critical services.

Understanding how to manage processes on your server is a fundamental skill for maintaining performance and troubleshooting issues. One of the most important tools for this is the kill command.

What is the kill command?

The kill command is used to send signals to processes running on your server. Think of a “signal” as a message that tells a process to do something specific, like gracefully shut down or immediately stop.

  • Default Signal (TERM): If you don’t specify a signal, kill sends the TERM (Terminate) signal (signal number 15). This is a polite request for the process to shut down. Most well-behaved applications will catch this signal, save their work, and exit cleanly. This is the preferred method for stopping processes.
  • Forceful Signal (KILL): Sometimes, a process might not respond to the TERM signal. In such cases, you might need to use the KILL signal (signal number 9). This is a much more forceful command that the process cannot ignore or catch, forcing it to stop immediately. Use KILL as a last resort, as it doesn’t allow the process to save its state, which could lead to data corruption in some applications.

Most modern command-line shells include a built-in kill function that works very similarly to the standalone kill command.

Advanced PID targeting and options

Beyond targeting a single process by its PID, kill offers more advanced ways to specify which processes receive the signal.

  • Targeting by Process ID ( pid ):
    • n (where n is greater than 0): This is the most common use, signaling the single process with the specified Process ID.
    • 0 : This signals all processes that belong to the current process group.
    • -1: This signals all processes on the system with a PID greater than 1. Use this with extreme caution, as it can disrupt your entire server.
    • -n (where n is greater than 1): This signals all processes in the specified process group n. When using a negative number to denote a process group, you must either specify the signal first (e.g., kill -SIGTERM -2500) or precede the process group number with -- (e.g., kill -- -2500) to avoid it being interpreted as a signal number.
  • -a (Do Not Restrict by User ID): When signaling processes by their command name (e.g., using killall or pkill), this option allows you to target processes run by any user, not just processes run by your current user ID.
  • -p (Print PID Only): This option tells kill to simply print the Process ID(s) of the named processes instead of sending any signals. This is a very useful way to safely check which processes would be affected by a kill command before actually sending a signal.
Key options and usage examples

To use kill, you typically need to know the Process ID (PID) of the process you want to manage. You can find PIDs using commands like ps (e.g., ps aux | grep <process_name>) or pgrep (e.g., pgrep <process_name>). If you’re working with background jobs in your current shell, you can use the jobs command to find their Job IDs.

⚠️ Important Warning: Using the kill command incorrectly can have serious consequences, including crashing critical server services or even making your server inaccessible. Always double-check the Process ID (PID) before issuing a kill command, especially with the -9 (KILL) signal. When in doubt, consult with Liquid Web’s Support® team.

Here’s how to use kill in common scenarios:

Gracefully terminate a process (Using PID)

This is the standard and safest way to stop a process, allowing it to clean up before exiting.

Command:

kill <PID>

Example: To gracefully stop a process with PID 23125:

kill 23125
Forcefully terminate a process (Using PID)

If a process isn’t responding to a graceful termination, you can use the KILL signal.

Command:

kill -9 <PID>

Example: To forcefully stop a process with PID 1283:

kill -9 1283
Terminate a process using a signal name

You can also specify signals by their name (e.g., SIGTERM, SIGKILL) instead of their number.

Command:

kill -SIGTERM <PID>

Example: To gracefully stop a process with PID 834 using the signal name:


kill -SIGTERM 834
Terminate a background job (Using Job ID)

If you have processes running in the background of your current shell (listed by the jobs command), you can refer to them by their Job ID.

Command:

kill %<JOB_ID>

Example: To gracefully stop the background job with Job ID 1:

kill %1

Note: The % symbol is required when using a Job ID.

Terminate processes by command name

You can send a signal to all processes invoked with a specific command name. This is useful if you want to stop all instances of a particular application.

Command:

killall <command_name>

Example: To gracefully stop all PHP processes:

killall php

Note: While the core kill command can directly target by name (often requiring the -a option for processes not owned by your user), killall is a more commonly used and straightforward command for this purpose.

List available signals

To see a list of all available signal names and their corresponding numbers, use the -l option:

kill -l

This will output a list like 1) SIGHUP 2) SIGINT 3) SIGQUIT ... 9) SIGKILL ... 15) SIGTERM ....

Conclusion

The kill command is an essential tool for any server administrator. By understanding how to send different signals to processes, you gain precise control over your server’s operations, allowing you to gracefully manage applications or forcefully stop unresponsive ones when necessary. If you ever need assistance managing processes on your Liquid Web server, our Support team is always here to help!

Was this article helpful?