Linux Kill Command
The kill command is an excellent command to terminate a process directly without having to reboot or logging out the computer. Before killing a process, it would be wise to know what is process. Process is known as a running instance of a program, which can also be called as task executing or running. These running instance so called process are assigned by a unique process identification number called (PID). The assigning of the process identification number (PID) is automatically created by the system in order to refer to the process at any instance.
Syntax:
kill [signal or option] PID Number
It is to be noted that the process identification number (PID) must be correct. Also more than one PID can be provided within a single command.
$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
joe 1981 0.0 0.9 293632 27808 ? S 14:55 0:00 /usr/bin/kwalletd –pam-login 7 10
joe 1986 0.0 1.1 370144 34108 ? Ss 14:55 0:00 kdeinit4: kdeinit4 Running…
joe 1987 0.0 0.7 373576 24308 ? S 14:55 0:00 kdeinit4: klauncher [kdeinit]
For example, if you want to kill a process with a process identification number (PID) known:
Kill -9 1987
Sometimes if a user doesn’t know the process identification number (PID) for a particular process, but knows the program running, then the command killall comes into the picture.
$ killall -9 chrome
or
$ killall -9 php
What are signals
The inter-process communication in Linux and Unix like operating systems are called signals. This allows to communicate or exchange data among process or commands having multiple threads. While sending a signal to a process or command an event is generated. Trap command is used to catch signals and handle errors in a Linux shell scripts. Different signals can be generated and sent to commands or processes.
What are process:
Linux is an operating system which can accommodate many users and can allot resource sharing to all of the users. This feature allows you to workout various commands and many tasks simultaneously. Process identification number (PID) is always assigned to a process when a process begins. The range of process identification number (PID) is 0 to 65535. The process identification number (PID) 1 will be always assigned to init process. Init process is the initial process initiated at the time of booting.
The example below shows process identification number (PID) of init as one
$ ps -C init -o pid=,cmd
Output:
CMD
1 /sbin/init
What are process states:
The various life cycles of a process in Linux are:
1) Creation
2) Execution
3) Termination
4) Removal
There are various states for each process generated which defines the current details or what is happening with that process. The states of the process are:
1) R (running): Process is running or executing.
2) S (sleeping): Process is not running and is waiting for an event or a signal.
3) T (traced or stopped): Process is stopped by signals such as SIGINT or SIGSTOP.
4) D (uninterruptible sleep): Process is sleeping and it won’t get back until an event such as I/O occurred.
5) Z (zombie or defunct): Processes marked are dead processes (so-called “zombies”) that remain because their parent has not destroyed them properly.
How to view Processes
In Linux processes can be viewed by the commands:
- ps
- pstree
- pgrep: pgrep command places a snapshot of the current process.
$ ps
PID TTY TIME CMD
10619 pts/9 00:00:00 bash
10634 pts/9 00:00:00 ps
$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 33892 2004 ? Ss 14:55 0:02 /sbin/init
root 2 0.0 0.0 0 0 ? S 14:55 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 14:55 0:02 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< 14:55 0:00 [kworker/0:0H]
$ pstree
init─┬─ModemManager───2*[{ModemManager}]
├─NetworkManager─┬─dhclient
│ ├─dnsmasq
│ └─3*[{NetworkManager}]
├─accounts-daemon───2*[{accounts-daemon}]
├─acpid
├─avahi-daemon───avahi-daemon
├─bluetoothd
├─colord───2*[{colord}]
├─console-kit-dae───63*[{console-kit-dae}]
If you need any further assistance please contact our support department.