What is a process
A process, also known as a task, is the running form of a program. Programs are stored on disk and processes run in memory. Processes have a parent/child relationship. A process can spawn one or more children. Multiple processes can run in parallel.
Listing System Processes
The process status (ps) command lists the processes that are associated with your shell.
# ps [options]
For each process, the ps command displays the PID, the terminal identifier (TTY), the cumulative execution time (TIME), and the command name (CMD). For example, list the currently running processes on the system using the ps command.
# ps PID TTY TIME CMD 1442 pts/1 00:00:00 sudo 1448 pts/1 00:00:00 su 1449 pts/1 00:00:00 bash 1666 pts/1 00:00:00 ps
The ps command has several options that you can use to display additional process information.
- -a: Prints information about all processes most frequently requested, except process group leaders and processes not associated with a terminal
- -e: Prints information about every process currently running
- -f: Generates a full listing
- -l: Generates a long listing
- -o format: Writes information according to the format specification given in a format. Multiple -o options can be specified. The format specification is interpreted as the space-character-separated concatenation of all the format option arguments.
Listing All Processes
For example, use the ‘ps -ef‘ command to list all the processes currently scheduled to run on the system.
# ps -ef | more
Here,
- UID: The username of the owner of the process
- PID: The unique process identification number of the process
- PPID: The parent process identification number of the process
- STIME:The time the process started (hh:mm:ss)
- TTY:The controlling terminal for the process. Note that system processes (daemons) display a question mark (?), indicating the process started without the use of a terminal.
- TIME: The cumulative execution time for the process
- CMD: The command name, options, and arguments
Terminating a Process
There might be times when you need to terminate an unwanted process. A process might have got into an endless loop, or it might have hung. You can kill or stop any process that you own. You can use the following two commands to terminate one or more processes:
– kill
– pkill
The kill and pkill commands send signals to processes directing them to terminate. Each signal has a number, name, and an associated event. Below are some of the most commonly used signals with their functionalities.
Number | Name | Description |
---|---|---|
1 | SIGHUP | Reload Configuration File |
2 | SIGINT | Interrupt by keyboard (ctrl+c) |
9 | SIGKILL | kill process |
15 | SIGTERM | End process immediately. (Terminate a process in controlled manner so cleanup is possible) |
18 | SIGCONT | Continue the process stopped with STOP |
19 | STOP | Stop process |
Terminating a Process using kill Command
You can terminate any process by issuing the appropriate signal to the process concerned. The kill command sends signal 15, the terminate signal, by default. This signal causes the process to terminate in an orderly manner. The kill command sends a termination signal to one or more processes. The syntax to use the kill command is as follows :
# kill [-signal] PIDs
You need to know the PID of the process before you can terminate it. You can use either the ps or pgrep command to locate the PID of the process. Also, you can terminate several processes at the same time by entering multiple PIDs on a single command line. Lets see an example of kill command. We would kill the process ‘sleep 400’ as shown below.
# ps -ef | grep sleep root 1337 1218 0 07:33 pts/0 00:00:00 sleep 400
# kill -9 1337
# ps -ef | grep sleep #
Terminating a Process using pkill or killall Command
Alternatively, you can use the pkill or killall command to send termination signal to processes.
# pkill [-options] PIDs
or
# killall [-options] PIDs
The pkill/killall command requires you to specify the name instead of the PID of the process. For example, use the pkill command to terminate the dtmail process.
# pkill dtmail # pgrep -l mail #
Forcefully Terminating a Process: Signal 9
Some processes ignore the default signal 15 that the kill command sends. If a process does not respond to signal 15, you can force it to terminate by using signal 9 with the kill or pkill command.
# kill -9 PID
or
# pkill -9 process_name