Eight commands in Linux to effectively manage a process

  • 2020-11-30 08:45:30
  • OfStack

preface

The role of process management:

Determine server health: Determine server load and security status by analyzing process state (memory, CPU occupancy, etc.) View all processes in the system Kill the process

This article will give you more details about Linux's process management commands, which are key commands to manage your application throughout the process.

In general, there are three main states in the life cycle of an application process: start, run, and stop. If we want to be competent administrators, every state can and should be managed carefully. These eight commands can be used to manage the entire life cycle of a process.

Start the process

The easiest way to start a process is to type its name on the command line and press enter. To start the Nginx web server, type nginx. Maybe you just want to see the version.


alan@workstation:~$ nginx

alan@workstation:~$ nginx -v
nginx version: nginx/1.14.0

View your executable path

The above demonstration of starting the process assumes that the executable is in your executable path. Understanding this path is key to reliably starting and managing processes. Administrators often customize this path for their desired purpose. You can use echo $PATH View your executable path.


alan@workstation:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin

WHICH

Use the which command to see the full path to the executable.


alan@workstation:~$ which nginx
/opt/nginx/bin/nginx

I'll use the popular web server software Nginx as my example. Suppose you have Nginx installed. If you execute which nginx If the command returns nothing, then Nginx cannot be found, because it only searches for the executable path that you specify. There are three ways to remedy a situation where a process cannot simply be started by name. The first is to type the full path -- although, I would rather not type the full path, would you?


alan@workstation:~$ /home/alan/web/prod/nginx/sbin/nginx -v
nginx version: nginx/1.14.0

The second solution is to install the application in a directory in the executable file path. However, this can sometimes be impossible, especially if you do not have root permissions.

The third solution is to update your executable path environment variables, including the installation directory of the particular application you want to use. This solution is related to shell. For example, Bash users need to edit PATH= lines in their.bashrc file.


PATH="$HOME/web/prod/nginx/sbin:$PATH"

Now, repeat your echo and which commands or try checking the version. Much easier!


alan@workstation:~$ echo $PATH
/home/alan/web/prod/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin

alan@workstation:~$ which nginx
/home/alan/web/prod/nginx/sbin/nginx

alan@workstation:~$ nginx -v            
nginx version: nginx/1.14.0

Keep the process running

NOHUP

When the terminal is logged off or closed, the process may not continue to run. This special case allows the process to continue running by placing the nohup command in front of the command to be run. In addition, add 1 & The symbol will send the process to the background and allow you to continue using the terminal. For example, suppose you want to run myprogram.sh.


nohup myprogram.sh &

nohup returns PID for the running process. I'm going to talk more about PID.

Manage running processes

Each process has a unique process id number (PID). This is the number we use to manage each process. We can also use process names, which I'll demonstrate below. There are several commands to check the status of a running process. Let's take a quick look at these commands.

PS

The most common is the ps command. The default output of ps is a simple list of processes running on the current terminal. Column 1 contains PID, as shown below.


alan@workstation:~$ ps
PID TTY   TIME CMD
23989 pts/0 00:00:00 bash
24148 pts/0 00:00:00 ps

I want to take a look at the Nginx process I started earlier. To do this, I told ps to show me each running process (-ES95en) and the full list (-f).


alan@workstation:~$ ps -ef
UID  PID PPID C STIME TTY   TIME CMD
root   1  0 0 Aug18 ?  00:00:10 /sbin/init splash
root   2  0 0 Aug18 ?  00:00:00 [kthreadd]
root   4  2 0 Aug18 ?  00:00:00 [kworker/0:0H]
root   6  2 0 Aug18 ?  00:00:00 [mm_percpu_wq]
root   7  2 0 Aug18 ?  00:00:00 [ksoftirqd/0]
root   8  2 0 Aug18 ?  00:00:20 [rcu_sched]
root   9  2 0 Aug18 ?  00:00:00 [rcu_bh]
root  10  2 0 Aug18 ?  00:00:00 [migration/0]
root  11  2 0 Aug18 ?  00:00:00 [watchdog/0]
root  12  2 0 Aug18 ?  00:00:00 [cpuhp/0]
root  13  2 0 Aug18 ?  00:00:00 [cpuhp/1]
root  14  2 0 Aug18 ?  00:00:00 [watchdog/1]
root  15  2 0 Aug18 ?  00:00:00 [migration/1]
root  16  2 0 Aug18 ?  00:00:00 [ksoftirqd/1]
alan  20506 20496 0 10:39 pts/0 00:00:00 bash
alan  20520 1454 0 10:39 ?  00:00:00 nginx: master process nginx
alan  20521 20520 0 10:39 ?  00:00:00 nginx: worker process
alan  20526 20506 0 10:39 pts/0 00:00:00 man ps
alan  20536 20526 0 10:39 pts/0 00:00:00 pager
alan  20564 20496 0 10:40 pts/1 00:00:00 bash

You can see the Nginx process in the output of the ps command above. This command shows nearly 300 lines, but I've shortened it in this example. As you can imagine, trying to process 300 lines of process information is a bit confusing. We can feed this output to grep and only show nginx under filter 1.


alan@workstation:~$ ps -ef |grep nginx
alan  20520 1454 0 10:39 ?  00:00:00 nginx: master process nginx
alan  20521 20520 0 10:39 ?  00:00:00 nginx: worker process

It's better. As we can see very quickly, Nginx has PID for 20520 and 20521.

PGREP

The pgrep command simplifies the problems encountered when calling grep alone.


alan@workstation:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
0

Suppose you are in a managed environment and multiple users are running several different instances of Nginx. You can use the -ES121en option to exclude others from the output.


alan@workstation:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
1

PIDOF

The other one that works is the pidof. This command checks PID for a particular base 2 file, even if another process with the same name is running. To create an example, I copy my Nginx to the second directory and start it with the appropriate path prefix. In real life, this instance might be in a different location, such as a directory owned by different users. If I run two instances of Nginx, the pidof output shows all of their processes.


alan@workstation:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
2

Using grep or pgrep will display the PID number, but we may not be able to tell which instance is which.


alan@workstation:~$ pgrep nginx
20881
20882
20895
20896

The pidof command can be used to determine PID for each specific INSTANCE of Nginx.


alan@workstation:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
4

TOP

The top command has a long history and is useful for looking at the details of running processes and quickly identifying issues such as memory consumption. Its default view is shown below.


alan@workstation:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
5

You can change the update interval by typing the letter s and your preferred number of update seconds. To make it easier to monitor our sample Nginx process, we can call top with the -ES159en option and pass PID. This output is much cleaner.


alan@workstation:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
6

When managing a process, and especially when terminating a process, it is important to correctly identify PID. In addition, if top is used in this way, top needs to be informed that there is a new process every time one of these processes stops or a new process starts.

Terminate the process

KILL

Interestingly, there is no stop command. In Linux, there is the kill command. kill is used to send signals to processes. The most common signal is "abort" (SIGTERM) or "kill" (SIGKILL). However, there is more. Here are some examples. The complete list can be displayed as ES183en-L.


alan@workstation:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
7

Notice that the number 9 signal is SIGKILL, and usually, we'll say something like kill -9 20896 Such an order. The default signal is 15. This is SIGTERM. Keep in mind that many applications have their own stopping methods. Nginx transmits signals using the -s option, such as stop or reload. In general, I prefer to use application-specific methods to stop operations. However, I will demonstrate using the kill command to stop the Nginx process 20896, and then using pgrep to confirm that it has stopped. PID 20896 no longer appears.


alan@workstation:~$ kill -9 20896
 
alan@workstation:~$ pgrep nginx
20881
20882
20895
22123

PKILL

The command pkill is similar to pgrep in that it can be searched by name. This means that you must be very careful when using pkill. In my Nginx example, if I wanted to kill only one instance of Nginx, I might not choose to use it. I can either pass the Nginx option -ES212en stop to a specific instance to eliminate it, or I need to use grep to filter the entire ps output.


alan@workstation:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
9

If I wanted to use pkill, I could include the -f option to let pkill filter the entire command line argument. This certainly applies to pgrep. So, in execution pkill -f Before, first of all I can use pgrep -a Confirm 1.


alan@workstation:~$ pgrep -a nginx
20881 nginx: master process ./nginx -p /home/alan/web/prod/nginxsec
20882 nginx: worker process
20895 nginx: master process nginx
20896 nginx: worker process

I can use it, too pgrep -f Narrow down my results. pkill using the same parameter stops the process.


alan@workstation:~$ pgrep -f nginxsec
20881
           
alan@workstation:~$ pkill -f nginxsec

The key point to keep in mind for pgrep (and pkill in particular) is that you must always ensure that the search results are accurate so that you do not inadvertently affect the wrong process.

Most of these commands have many command-line options, so I always recommend reading the man man page for each command. While most of these commands exist on Linux, Solaris, and BSD platforms, there are a few differences. When working on the command line or writing scripts, always test and be ready to correct as needed.

via: https://opensource.com/article/18/9/linux-commands-process-management

Author: Alan ES247en-ES248en topic: lujun9972 Translator: heguangzhi proofreading: wxy

conclusion


Related articles: