Terminate Processes in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to terminate processes in Linux using the kill, killall, and pkill commands. These commands are essential for managing running processes and can be used to stop unresponsive applications or free up system resources.

By the end of this lab, you'll be able to:

  • Use the kill command to terminate a specific process
  • Use the killall command to terminate multiple processes by name
  • Use the pkill command as an alternative to killall

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux(("`Linux`")) -.-> linux/ProcessManagementandControlGroup(["`Process Management and Control`"]) linux/BasicSystemCommandsGroup -.-> linux/sleep("`Execution Delaying`") linux/TextProcessingGroup -.-> linux/grep("`Pattern Searching`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/SystemInformationandMonitoringGroup -.-> linux/ps("`Process Displaying`") linux/ProcessManagementandControlGroup -.-> linux/kill("`Process Terminating`") linux/ProcessManagementandControlGroup -.-> linux/killall("`Multi-Process Killing`") linux/ProcessManagementandControlGroup -.-> linux/pkill("`Pattern-Based Killing`") subgraph Lab Skills linux/sleep -.-> lab-44{{"`Terminate Processes in Linux`"}} linux/grep -.-> lab-44{{"`Terminate Processes in Linux`"}} linux/sudo -.-> lab-44{{"`Terminate Processes in Linux`"}} linux/ps -.-> lab-44{{"`Terminate Processes in Linux`"}} linux/kill -.-> lab-44{{"`Terminate Processes in Linux`"}} linux/killall -.-> lab-44{{"`Terminate Processes in Linux`"}} linux/pkill -.-> lab-44{{"`Terminate Processes in Linux`"}} end

Understanding Process IDs and Signals

Before we start terminating processes, it's important to understand two key concepts: Process IDs (PIDs) and signals.

  1. Open a terminal in your Ubuntu VM. You should be in the /home/labex/project directory by default. If you're not sure, you can always check your current directory by typing pwd and pressing Enter.

  2. Let's start by running a simple background process. Enter the following command:

    sleep 1000 &

    This command starts a process that will sleep for 1000 seconds and runs it in the background. The & at the end tells the shell to run the command in the background, allowing you to continue using the terminal.

  3. To see the running processes, use the ps command:

    ps aux | grep sleep

    This command shows all running processes (ps aux) and then filters the output to show only lines containing "sleep" (grep sleep).

    You should see output similar to this:

    lab-kill-process-1-1.png

    The number in the second column is the Process ID (PID). We'll use this to terminate the process later.

  4. Linux uses signals to communicate with processes. The most common signals for terminating processes are:

    • SIGTERM (15): Gracefully terminate the process. This is the default signal sent by the kill command.
    • SIGKILL (9): Forcefully kill the process. This should be used as a last resort when SIGTERM doesn't work.

Now that we understand PIDs and signals, we're ready to move on to using the kill command.

Using the kill Command

The kill command is used to send a signal to a specific process, identified by its PID. By default, it sends the SIGTERM signal, which asks the process to terminate gracefully.

  1. Let's terminate the sleep process we started earlier. First, find its PID using the ps command:

    ps aux | grep sleep

    Note down the PID of the sleep process. It will be a number in the second column of the output.

  2. Now, use the kill command to terminate the process:

    kill <PID>

    Replace <PID> with the actual Process ID you noted. For example, if the PID is 1234, you would type kill 1234.

  3. Check if the process is still running:

    ps aux | grep sleep

    You should no longer see the sleep process in the output, except for the grep command itself.

If the process is still running (which is unlikely for a simple sleep command, but possible for more complex applications), you can use a stronger signal:

kill -9 <PID>

This sends the SIGKILL signal, which forcefully terminates the process. Be cautious with SIGKILL as it doesn't allow the process to clean up, which might lead to data loss or other issues in more complex applications.

Using the killall Command

The killall command allows you to terminate processes by name, which can be more convenient than using PIDs, especially when you have multiple instances of the same program running.

  1. Start multiple sleep processes:

    sleep 2000 &
    sleep 2000 &
    sleep 2000 &

    This command starts three separate sleep processes, each running for 2000 seconds in the background.

  2. Verify that the processes are running:

    ps aux | grep sleep

    You should see three sleep processes listed.

  3. Now, use the killall command to terminate all sleep processes:

    sudo killall sleep

    We use sudo here because killall may need elevated privileges to terminate processes started by other users or system processes. You may be prompted to enter your password.

  4. Verify that the processes have been terminated:

    ps aux | grep sleep

    You should no longer see any sleep processes in the output, except for the grep command itself.

If you receive a "Operation not permitted" error, it means you don't have the necessary permissions to kill certain processes. In this case, using sudo as shown above should resolve the issue.

Using the pkill Command

The pkill command is another way to terminate processes by name, similar to killall. It's particularly useful when you want to terminate processes based on partial name matches.

  1. Start a few more sleep processes:

    sleep 3000 &
    sleep 3000 &
    sleep 3000 &

    This starts three more sleep processes, each running for 3000 seconds in the background.

  2. Use the pkill command to terminate these processes:

    sudo pkill sleep

    Like with killall, we use sudo to ensure we have the necessary permissions to terminate all matching processes.

  3. Verify that the processes have been terminated:

    ps aux | grep sleep

    You should not see any sleep processes in the output, except for the grep command itself.

The main difference between pkill and killall is that pkill can match on a partial process name. For example, pkill fire would kill both firefox and firebird processes. This can be both powerful and dangerous, so always double-check your pkill commands before running them.

Practice with Real Applications

Now that you've learned how to use kill, killall, and pkill, let's practice with a real application. We'll use Firefox as an example, you must practice this step on the desktop environment.

alt text
  1. Start the Firefox web browser:

    firefox &

    This command launches Firefox in the background. You should see the Firefox window open on your desktop.

  2. Use the ps command to find Firefox's PID:

    ps aux | grep firefox

    Note down the PID of the main Firefox process. It will typically be the first Firefox process listed.

  3. Use the kill command to terminate Firefox:

    kill <PID>

    Replace <PID> with Firefox's actual Process ID that you noted down.

  4. If Firefox doesn't close (which is possible as some applications catch the SIGTERM signal), try using the SIGKILL signal:

    kill -9 <PID>

    Remember, SIGKILL should be used as a last resort as it doesn't allow the application to save its state or clean up properly.

  5. Alternatively, you can use killall or pkill:

    sudo killall firefox

    or

    sudo pkill firefox

    These commands will terminate all Firefox processes at once, which can be more convenient than killing processes one by one.

Remember, forcefully terminating applications can lead to data loss, so it's always better to close applications normally when possible. These commands should be used when an application is unresponsive or when you need to quickly free up system resources.

Summary

Congratulations! You've successfully completed this lab on terminating processes in Linux. You've learned how to:

  1. Use the kill command to terminate a specific process by its PID
  2. Use the killall command to terminate multiple processes by name
  3. Use the pkill command as an alternative to killall
  4. Apply these commands to both simple background processes and real applications

Remember, while these commands are powerful tools for managing processes, use them cautiously. Terminating critical system processes can lead to system instability. Always verify the process you're terminating before using these commands.

As you continue your Linux journey, you'll find these process management skills invaluable for maintaining a smooth-running system and troubleshooting issues. Keep practicing these commands to become more comfortable with process management in Linux!

Other Linux Tutorials you may like