How to Stop Process Using Kill Command in Linux

The kill command is used on Linux and other Unix-like operating systems (Solaris, AIX, HP-Unix, Mac). It doesn’t matter which operating system you use to terminate the process without having to log off or restart your computer. A process, also called a task, is an execution (ie, running) instance of a program. When each process is created for reference by the system, a unique process identification number (PID) is automatically assigned to each process.

The syntax of kill is

kill [signal or option] PIDNumber

The only required parameter (ie input) is PID, and any number of PIDs can be used in a single command. The kill command will terminate the process using the kill signal and PID provided by the user.

$ sh ImE.sh &
 [1] 13452
$ jobs
 [1]+ Running sh ImE.sh &
$ jobs -l 1
 [1]+ 13452 Running ImE.sh &
$ kill -9 13452

The killall command kills all processes with a specific name. If the Firefox or java program is running multiple times and the user does not know the PID, use the killall command killall -9 firefox Either killall -9 java

$ ps
 PID TTY TIME CMD
 13156 pts/0 00:00:00 bash
 13309 pts/0 00:00:00 sh
 13310 pts/0 00:00:05 java
 13452 pts/0 00:00:00 sh
 13453 pts/0 00:00:05 java
$ killall java

table of Contents

    • How the kill command works
    • Definition of process
    • Parent-child process
    • Process status
    • List active processes
  • 1) Terminate the process by pid
  • 2) Kill processes by name
  • 3) Kill all processes in Linux
  • 4) Termination process
  • 5) Kill and stop running jobs in the background
  • 6) How to find and kill zombie processes
  • 7) How to find and kill all sleep processes
  • 8) How to find and kill all STOP processes
    • Also read:

How the kill command works

Linux also supports POSIX Reliable signals (“standard signals”) and POSIX real-time signals. In Linux and Unix operating systems, signals are nothing more than some kind of inter-process communication (a technique for exchanging data between one or more processes or multiple threads in a command). Signals are sent to a process or command to notify what happened. For example, running ls -R /, You might click CTRL+C (Or Break) to cancel command execution. You hit CTRL+C, One called SIGINT (2) Sent to indicate keyboard interruption. When SIGINT is sent to the ls command, Linux will interrupt the normal execution of the process. In this example, the ls command is terminated.

However, you can register a signal handler for CTRL+C And take some action when the ls command is interrupted by SIGINT, such as ignore it or display a message on the screen. You need to use the trap command to capture signals and handle errors under a Linux shell script. You can send various signals to commands and procedures. For example, to terminate the foreground process, you can click Ctrl+C Key combination. To kill background processes, you can use the kill command and send signal (Termination order):

$ kill -TERM pid
$ kill -TERM 1234

To list the available signals for the kill command, enter:

# kill -l
 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX

The kill command sends all the above signals to commands and processes. However, they will only respond if the commands are programmed to recognize those signals. Particularly useful signals include:

1. SIGHUP (1)-A hangup was detected on the control terminal or the control process was terminated. 2. SIGINT (2)-keyboard interrupt. 3. SIGKILL (9)-Termination signal, which terminates the running process. 4. SIGSTOP (19)-Stops the process. 5. SIGCONT (18)-If stopped, continue processing.

Definition of process

Linux is multi-user (multiple users can log in to Linux and share its resources) and multi-tasking operating system. This means that you can run multiple commands and perform multiple tasks at once.

For example, you can write a C program while listening to mp3, or use P2P to download files in the background. A process is a program (or a command typed by a user) that performs a specific job. In Linux, when you start a process, it is assigned a unique number called PID or process-id. PID from 0 to 65535. PID 1 It is always assigned to the init process, which is the first process started at startup.

Parent-child process

The parent process is a Linux process that has created one or more child processes. A process can spawn a child process, that is, create a child process. For example, if a user types ls at a shell prompt. The shell executes the ls command. The Linux kernel will copy the memory pages of the shell and execute the ls command.

In UNIX, each process is created using the fork and exec methods. However, this model causes a waste of system resources. Under Linux, the fork method is implemented using copy-on-write pages, so its only cost is the time and memory required to copy the parent’s page tables and create unique task structures for the children. Copy-on-write model avoids creating unnecessary structural copies when creating new processes. For example, a user types ls at a shell prompt. The Linux kernel will fork and create a child process or child process of bash. In this example, bash is the parent and ls command is the child. In other words, the ls command points to the same memory page as the bash shell. The child then executes the ls command using copy-on-write.

Process status

Each Linux process has its own life cycle, such as creation, execution, termination, and deletion. Each process has its own status, which shows what is currently happening in the process. The status of the process, which can be one of the following:

  • D (Uninterrupted Sleep)-The process is sleeping and can only resume after an event such as I / O. For example, the process foo is a process waiting for a keyboard interrupt.
  • R (running)-The process is running or executing.
  • S (sleep)-The process is not running and is waiting for an event or signal.
  • T (trace or stop)-The process was stopped by a signal such as SIGINT or SIGSTOP.
  • Z (zombies or disappeared)-Marked dead processes (so-called “zombies”) are dead processes because their parent process did not destroy them properly. If the parent process exits, these processes will be destroyed by init.

You can use ps command to view the status of the process as shown below

ps -C processName -o pid=,cmd,stat

For example, to display the status of the lighttpd, php-cgi, and firefox-bin processes, enter:

$ ps -C chrome -o pid=,cmd,stat
 CMD STAT
 4423 /opt/google/chrome/chrome SLl+
 4434 /opt/google/chrome/chrome - S+
 4439 /opt/google/chrome/chrome - S+
$ ps -C pulseaudio -o pid=,cmd,stat
 CMD STAT
 3674 /usr/bin/pulseaudio --start S

List active processes

To see what processes are running on your system, you can use some commands. Therefore, you need to use the ps command, pstree command, and pgrep command.

  • ps : This command displays information about active processes. It is used to view the current process
# ps
 PID TTY TIME CMD
 15013 pts/2 00:00:00 su
 15019 pts/2 00:00:00 bash
 15169 pts/2 00:00:00 ps

Print all processes owned by the user

# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.4 43244 2336 ? Ss Apr13 0:47 /usr/lib/sys
root 2 0.0 0.0 0 0 ? S Apr13 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S Apr13 0:12 [ksoftirqd/0
root 5 0.0 0.0 0 0 ? S< Apr13 0:00 [kworker/0:0
root 7 0.0 0.0 0 0 ? S Apr13 0:00 [migration/0

Print all processes owned by the user and use grep command to sort specific processes

# ps aux | grep "mysqld"
 root 15189 0.0 0.0 103308 868 pts/2 S+ 10:26 0:00 grep mysqld
  • pstree: This command displays running processes as a tree. If pid is omitted, the root of the tree is pid or init.
# pstree
 init─┬─BESClient─┬─XBESClientUI───2*[{XBESClientUI}]
 │ └─5*[{BESClient}]
 ├─NetworkManager───{NetworkManager}
 ├─acpid
 ├─agnLogd
 ├─agnclientd───{agnclientd}
 ├─atd
 ├─auditd───{auditd}
 ├─automount───4*[{automount}]
 ├─blue-coat-unifi───32*[{blue-coat-unif}]
 ├─bluetoothd
 ├─bonobo-activati───{bonobo-activat}
 ├─ciscod
 ├─clock-applet
 ├─console-kit-dae───63*[{console-kit-da}]
 ├─crond
 ├─cups-ipm-confid
 ├─cupsd
 ├─3*[dbus-daemon───{dbus-daemon}]
 ├─3*[dbus-launch]
 ├─

1) Terminate the process by pid

You can use pkill command, kill command or killall command to send various signals to commands / processes and shell scripts. The kill command is used to send a signal to a process using its pid. To send the termination signal to PID # 1234, you can use some methods:

# kill -9 1234
# kill -KILL 1234
# kill -SIGKILL 1234

2) Kill processes by name

The killall command sends a signal to all processes running any specified command. If no signal name is specified, SIGTERM is sent. You can terminate all processes (child and parent) of a particular process.

For example, let's search for the process to kill

$ ps -ef|grep -i chrome
franhes+ 4423 1 6 Apr26 tty2 00:13:56 /opt/google/chrome/chrome
franhes+ 4436 4434 0 Apr26 tty2 00:00:00 /opt/google/chrome/nacl_helper

Now we can terminate the process

# killall chrome

The pkill command also allows you to terminate the program just by specifying the name. It can kill all commands with the same process ID. You may have executed a specific script or command but want to kill it. You can get its pid with the pgrep command to make sure the process is running and kill it with the pkill command.

For example, we launch the following script

# ./continue
What is the difference between a duck and a bird ?
(Press Enter to see your response)

Imagine we forgot the script and we want to kill it later, we need to determine if it is still running. We will use pgrep -f Have the pid of the process that matches the full command line we executed earlier. it is -f Option

# pgrep -f './continue'
20037

Now we can kill the complete command with its name pkill -f. of -f option is used to terminate the entire command line.

# pkill -f './continue'

3) Kill all processes in Linux

I said you may need to kill all processes on your system all This process is very surprising and is not recommended. For this you can use killall5 -9. It sends signals to all processes except kernel threads and processes in its own session, so it does not kill the shell from which the script is called. killall5 is the SystemV killall command.

$ killall5 -9

You can kill all processes for a given user using one of the following methods:

$ killall -u username
$ pkill -u username

Or you can use numeric UID instead of username. Note that on some systems (such as Solaris), killall functions are different (similar to killall5). The pkill command is another command with additional options that can terminate a process by its name, username, group name, terminal, UID, EUID, and GID. It will send the specified signals to each process (the default is SIGTERM) instead of listing them on stdout.

pkill - kill process

To send a termination signal to the php-cgi process, enter:

$ pkill -KILL php-cgi

The above example will kill the php-cgi process for all users. However, -u The option will only kill processes whose effective user ID is set to shaha:

$ pkill -KILL -u shaha php-cgi

4) Termination process

Normally, all processes terminate themselves. In this example, the find command will terminate when it completes its task:

# find /home -name "*.c" 2>error.log 1>filelists &
  • you can Terminate foreground process By pressing CTRL+C. It will send a TERM signal to the process. For example, run the following command
# ls -R /
  • to Simple termination Press CTRL+C Send an in interrupt signal to the ls command. To terminate unwanted background processes, use the kill command with the following command: -9 The signal is sent to the processing section as described:
# kill -TERM pid
# kill -KILL pid
  • to Stop (hang) the foreground process Hit CTRL+Z. To resume the foreground process, use fg The command associated with the job number of the foreground process. In the following example, we will execute the command in the background:
$ sh ImE.sh &
 [1] 17565
# vim file10 &
[4] 20325

5) Kill and stop running jobs in the background

You can run multiple processes in the background while you continue to work in the foreground. You don't have to wait for the background process to finish before the shell can run other processes. To run the program in the background, just add the AND sign & At the end of the command. It's like running a minimized application.

The foreground process is the process that can display its execution progress or the interface through which the user can interact with the program. Sometimes, users must wait for one foreground process to complete before running another foreground process. To start the foreground process, enter the command at the prompt

# vim file10

When the s process is running in the foreground, you can terminate the process in the following ways: CTRL+C Key combination or use CTRL+Z (Put in foreground). We can convert the foreground process to the background process. After that, you can manage these background tasks (kill them, etc.) with Jobs commands. Please note that the background command is Do not Detach from your tty

# jobs
[1] Running ./continue
[2]- Running vim file10
[3]+ Running ./administration

We can use the following commands to manipulate background and foreground processes:

  • CTRL+C: Termination process
  • CTRL+Z: Pause the process
  • jobs: Show the status of all background and paused jobs
  • bg: Move foreground / suspend process to background
  • fg: Move background processes to the foreground

We move the script to the foreground

# fg %1
./continue

We will move the second script in the background

# bg %3
[3] ./administration &

You can use the following command to terminate the background running job kill -9 %job-number command

# kill -9 %1
[1]- Stopped ./continuer

View the results of a job command

# jobs
[1]  killed ./continue
[2]- Running vim file10
[3]+ Running ./administration

If you forget -9 Option, you will terminate the process. as follows

# kill %3
[3]- Stopped ./administration &

Now let's see the result

# jobs
[2]- Running vim file10
[3]+ Terminated ./administration

You can see with Terminated with Killed

Background jobs are just jobs that do n’t interact with the user. Ctrl+Z Is the most common default keyboard mapping for key sequences used to suspend the foreground process. The SIGSTOP signal is used to stop running background jobs. We can use pid or job-id.

jobs -l Pid showing process

# jobs -l
[3]- 20409 Running ./administration
[4]+ 20417 Running ./my_name

Now we can kill in two ways

$ kill -SIGSTOP 20409
$ kill -SIGSTOP %4

6) How to find and kill zombie processes

You can find zombie processes in the following ways ps aux | grep Z command. the process of z In the Status field is the zombie process:

# ps aux | grep Z
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 20430 0.0 0.1 112648 960 pts/1 R+ 04:15 0:00 grep --color=auto Z
# ps -A -ostat,ppid | awk '/[zZ]/{print $2}'
 3817
 4720
 4720

To kill the zombie process, find the zombie's parents PID (PPID) And send it to him SIGCHLD (17) Signal: kill -17 ppid I used the following command to find the PPID: ps -p PID -o ppid

# ps -p 20736 -o ppid
 PPID
 20735
# kill -17 20735

Either

# kill $(ps -A -ostat,ppid | awk '/[zZ]/{print $2}')

Note: If the parent process of the zombie process is killed, the zombie process will also die.

7) How to find and kill all sleep processes

a process Trumpet State is usually in a blocked system call, such as reading or writing a file or network, or waiting for another called program to complete.

you can use it strace -p pid_value Command to find out what system call is currently occurring. It will produce similar output

# ps -A -ostat,ppid | awk '/[sS]/{print $1 "t" $2}'
 STAT PPID
 S 0
 S 2
 S 2
 S 2
# kill -9 $(ps -A -ostat,ppid | awk '/[Ss]/{print $2}')

8) How to find and kill all STOP processes

A stopped job is a job that has been temporarily placed in the background and is no longer running, but is still using such resources (ie system memory). Because the job is not attached to the current terminal, it cannot produce output and does not receive input from the user.

# ps -A -ostat,ppid | awk '/[T]/{print $1 "t" $2}'
 STAT PPID
 T 18187
 Tl 18310
# kill -9 $(ps -A -ostat,ppid | awk '/[T]/{print $2}')

Use the kill command on Linux and other Unix-like operating systems to terminate processes without having to log off or reboot (ie, restart) the computer. Therefore, it is particularly important for the stability of such a system. A process, also called a task, is an execution (ie, running) instance of a program. When each process is created for reference by the system, a unique process identification number (PID) is automatically assigned to each process.

Also read:

  • 6 Actual usage of killall command in Linux
  • fkill-Interactive tool to kill processes in Linux
Sidebar