In this article, we are going to tackle a new topic: Managing Processes.
What is process?
A process is a program loaded in memory.
A typical process has:
- Process ID (PID): which is used to uniquely identify the process.
- Parent Process ID (PPID): the ID of the parent process that invoked this process.
- User ID (UID): the ID of the user who started the process.
Displaying Running Processes
To display the list of processes currently running on the system, the well-known command ps is used. ps has many options that control its output and the format of this output. In this section, we are going to learn the most popular of these options.
ps –ef ## Display all processes on the system ps –u username ## Display processes by user name or ID.
The pgrep (Process grep) Command
pgrep searches the currently running processes for a specific pattern, and writes the process(es) IDs that match the selection criteria to standard output.
pgrep [-d delimiter][-v] PATTERN
PATTERN is the pattern to search for.
delimiter is the string used to delimit each process ID in the output. If no delimiter specified, new line is used.
-v inverts the selection, i.e. the matching processes are discarded and those not matching are printed.
The following command will list the process ID for all processes that contain the pattern sshd:
To display the processes in the output separated by (for example) white space, we need to specify the delimiter:
The command pidof searches for the process ID of a running program by the exact program name.
program-name is the name of the program to look for its process(es).
The following command will list process IDs of the processes whose name is httpd.
kill, killall, and pkill
The kill command is used to terminate a process. Its usage is straightforward.
kill [–SIGNAL] PID …
PID is the ID of the process to be killed.
SIGNAL specifies the signal to send. The signal may be given as a signal name or number.
Usually, one of two signals is commonly used:
- SIGKILL or 9
Used to terminate process(s) immediately.
- SIGTERM or 15
killall –e [-SIGNAL] EXACTNAME killall –r [-SIGNAL] REGEX
EXACTNAME is the exact name of the process to be killed.
REGEX is a
We need to kill any process containing the string “ntp”. The following command will achieve this requirement.
We need to terminate any process whose name is exactly httpd. The following command will do this task:
Like killall, pkill terminates process(s) by name.
Starting Processes in the Background
There will be cases wherein you need to let a process work in the background. For example, when a process that takes a long time (like backup operation) is about to be executed, we could start it in the background, so that we get the prompt instantly, and don’t have to wait for the process to complete to do another work.
To start a process in the background, append an ampersand to the end of the command.
To start the yes command in the background, use the following line:
Listing Background Processes
To list processes running in the background, use the command jobs.
The output shows there is one process started in the background, and displays the command used to start the process.
Killing a Background Process
To kill a process running in the background, the same kill command is used. We can specify the process to be killed either using its PID (displayed when the process was started, 1102 in the example above) like any other process, or using the job number. In the above example, the job number of the yes command is 1.
nice and renice
From its manual page, nice is used to run commands with adjusted (customized) niceness value, which affects process scheduling. If used alone (without command to run), the nice command prints the current niceness.
What is Niceness?
Suppose you and me are waiting for the left. When it stops and its door is open, only one can enter through the door at a time. So, either you or I should go first, then the other.
If you let me pass first, then you are nicer than me, and vice versa.
Similarly, a process is said to be nicer than other processes if it doesn’t take higher priority than others. Taking higher priority (less nice value) means the process have better chance to utilize CPU cycles more than other processes.
Niceness ranges from -20 (least nice = highest priority) to 19 (most nice = lowest priority).
nice –n NICEVALUE COMMAND [ARG]
NICEVALUE is a number between -20 and 19.
COMMAND is the command to be run with the customized priority.
Suppose we need to start a new shell, with a less priority than normal.
To check the nice value of the newly-started shell, type the command without any arguments.
It is 2 as expected.
Now, let’s start any process from this shell. The new process will be a child for this shell, so it will inherit its nice value.
To check the running processes, execute the top command:
Notice the first prcess with PID 1449, that was started by the command yes. Check the NI (nice) value: it is 2. Notice also that another process has nice value of 2; it is the top process itself. This is normal because it is also started from this shell, so it has the same nice value of its parent.
For an already-running process, it is possible to change its nice value using the command renice.
renice –n NEWNICEVALUE –p PID
NEWNICEVALUE is the new nice value to be set for the running process.
PID is the ID of the process to which the nice value is to be changed.
For the shell started with nice value = 2, we need to decrease the nice value to be -3.
First, we need to know the process ID of the current shell. This value is stored in special variable $$.
Now, use renice to change the nice value.
The command output tells that the priority (nice value) has been changed from 2 to -3 for the specified process.
To check the current nice value of the shell:
Exactly as expected.
* * * * * *
In this article, we have talked about Managing Processes. Knowing how to manage processes is vital for a system admin. We have learned commands like ps, pgrep, pidof, kill, pkill, jobs, nice, and renice. We have also illustrated our talk by examples. I hope you find this article useful. See you in the next article.