Terminate Processes with kill, killall, and pkill

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to kill a process with the kill, killall and pkill command.

These commands are used to send a signal to a process. A signal is a software interrupt sent to a process or a group of processes to notify them of an event. Signals are used to notify processes of events such as keyboard input, terminal status changes, and child process termination.

Achievements

  • kill - Send a signal to a process
  • killall - Kill all processes by name
  • pkill - Kill processes by name

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) 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`"]) shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) linux/BasicSystemCommandsGroup -.-> linux/sleep("`Execution Delaying`") linux/InputandOutputRedirectionGroup -.-> linux/pipeline("`Data Piping`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") 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`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/AdvancedScriptingConceptsGroup -.-> shell/adv_redirection("`Advanced Redirection`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills linux/sleep -.-> lab-44{{"`Terminate Processes with kill, killall, and pkill`"}} linux/pipeline -.-> lab-44{{"`Terminate Processes with kill, killall, and pkill`"}} linux/redirect -.-> lab-44{{"`Terminate Processes with kill, killall, and pkill`"}} linux/grep -.-> lab-44{{"`Terminate Processes with kill, killall, and pkill`"}} linux/sudo -.-> lab-44{{"`Terminate Processes with kill, killall, and pkill`"}} linux/ps -.-> lab-44{{"`Terminate Processes with kill, killall, and pkill`"}} linux/kill -.-> lab-44{{"`Terminate Processes with kill, killall, and pkill`"}} linux/killall -.-> lab-44{{"`Terminate Processes with kill, killall, and pkill`"}} linux/pkill -.-> lab-44{{"`Terminate Processes with kill, killall, and pkill`"}} shell/cond_expr -.-> lab-44{{"`Terminate Processes with kill, killall, and pkill`"}} shell/adv_redirection -.-> lab-44{{"`Terminate Processes with kill, killall, and pkill`"}} shell/globbing_expansion -.-> lab-44{{"`Terminate Processes with kill, killall, and pkill`"}} end

Send Signal to Process

kill is a command used to send a signal to a process.

Linux include a number of signals that can be sent to a process. The most common signals are:

  • SIGTERM - Terminate process
  • SIGKILL - Kill process
  • SIGSTOP - Stop process
  • SIGCONT - Continue process

Tips: SIGKILL is the default signal if no signal is specified. In this lab, we just use SIGKILL to kill a process.

The kill command is used to send a signal to a process. The syntax is:

kill [options] <signal> <pid>

The signal is the signal to send to the process. The pid is the process id of the process to send the signal to.

To kill a process, you can use the kill command with the SIGKILL signal.

kill -9 <pid>

Tips: The -9 option is the same as SIGKILL.

You can use the ps command to get the process id of a process.

ps -ef | grep <process>

Tips: The grep command is used to filter the output of the ps command.

For example, to kill the sleep process, you can use the following command:

ps -ef | grep sleep

The output will be similar to:

lab-kill-process-1-1.png

The 613 is the process id of the sleep process. You can use the kill command to kill the process:

sudo kill -9 613
lab-kill-process-1-2.png

Tips: If you are not root user, must the sudo command to run the kill command as the root user.

Kill All Process with killall Command

kill can be used to kill a process by name. The killall command is used to kill all processes by name.

The syntax of the killall command is:

killall [options] <process>

The process is the name of the process to kill.

For example, to kill all nginx processes. You need to see the process id of the nginx process if you use the kill command. But you can use the killall command to kill all nginx processes, just use the following command:

sudo killall nginx

Kill All Process with pkill Command

The killall command is not available on all Linux distributions. If the killall command is not available, you can use the pkill command.

The syntax of the pkill command is:

pkill [options] <process>

The process is the name of the process to kill.

The command is the same as the killall command:

sudo pkill nginx

Summary

Congratulations! You have successfully completed this lab.

In this lab, you have learned how to kill a process with the kill, killall and pkill command.

Remember, Only kill a process if you are sure that the process is not needed. Killing a process can cause data loss or other problems.

Keep learning!

Other Linux Tutorials you may like