Shell ScriptingLearn Managing Processes in Linux Shell Scripting

Learn Managing Processes in Linux Shell Scripting

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:

  • Name
  • 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.
 
Syntax

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.
 
Syntax

pgrep [-d delimiter][-v] PATTERN

Where:           
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.
 
Example
The following command will list the process ID for all processes that contain the pattern sshd:
1

To display the processes in the output separated by (for example) white space, we need to specify the delimiter:
2

pidof
The command pidof searches for the process ID of a running program by the exact program name.
 
Syntax

pidof program-name

Where:           
program-name is the name of the program to look for its process(es).
 
Example
The following command will list process IDs of the processes whose name is httpd.
3

kill, killall, and pkill
The kill command is used to terminate a process. Its usage is straightforward.
 
Syntax

kill [–SIGNAL] PID …

Where:
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

Used to terminate process(s) gracefully.
 
Example
To kill the process whose PID is 713, use the following command:
4

killall
The killall command kills all specified processes by their name.
 
Syntax

killall –e [-SIGNAL] EXACTNAME
killall –r [-SIGNAL] REGEX

Where:
EXACTNAME is the exact name of the process to be killed.
REGEX is a
 
Example
We need to kill any process containing the string “ntp”. The following command will achieve this requirement.
5

Example
We need to terminate any process whose name is exactly httpd. The following command will do this task:
6
pkill
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.
 
Syntax

COMMAND &

Example
To start the yes command in the background, use the following line:
1.1
 
Learn the Basics of C Programming Language
 
Listing Background Processes
To list processes running in the background, use the command jobs.
7
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.
8nice 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).
 
Syntax

nice –n NICEVALUE COMMAND [ARG]

Where:            
NICEVALUE is a number between -20 and 19.
COMMAND is the command to be run with the customized priority.
 
Example
Suppose we need to start a new shell, with a less priority than normal.
9
To check the nice value of the newly-started shell, type the command without any arguments.
10
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.
11
To check the running processes, execute the top command:
12
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.
 
renice
For an already-running process, it is possible to change its nice value using the command renice.
 
Syntax

renice –n NEWNICEVALUE –p PID

Where:
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.
 
Example
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 $$.
13
Now, use renice to change the nice value.
14
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:
15
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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -