8 commands for efficient management of Linux processes
There are three main states in the life cycle of an application process: Start, run, and stop. If you want to be a competent Linux administrator, you need to know how to manage each state carefully. The following 8 commands can be used to help you manage the entire process lifecycle.
If you want to be a competent administrator, you need to know how to manage the state of your processes
Start the process
The easiest way to start a process is to type its name on the command line, and then press Enter. If you want to start the nginx web server, enter nginx. You will probably want to see its version.
alan@workstation:~ $nginx alan@workstation:~ $nginx -v nginx version:nginx/1.14.0
See execution path
The boot process demo above assumes that the executable is in your executable path. Understanding this path is the key to starting and managing processes reliably. Administrators often customize this path according to the purpose they want. You can use echo $ path to see the execution 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 of the executable.
alan@workstation:~ $which nginx /opt/nginx/bin/nginx
We will use the popular web server software nginx as an example. Let's say nginx is installed. If you execute the command which nginx and nothing is returned, then nginx cannot be found, since the command only looks for the executable path you specified. There are 3 ways to overcome the situation in which a process cannot be started by name. The first is to enter the full path:
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 under the executable path, however, this can sometimes fail, especially if you don't have root access.
The third solution is to update your executable path environment variable, including the installation directory for the specific application you want to use. This solution has to do with the shell. For example, the bash user needs to edit the line path = in the .bashrc file.
path="$home/web/prod/nginx/sbin:$path"
Now, repeat the commands echo and which or try to check the version.
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
Continue to run the process
nohup
When you log out or close Terminal, the process may not continue. In this particular case, you can keep the process running by placing the nohup command before the command you want to run. Also, add a trailing mark at the end and the process will be run in the background, allowing you to continue using Terminal. For example you want to run myprogram.sh.
nohup myprogram.sh&
nohup returns the pid of the running process.
Manage running processes
Each process has a unique identifier (pid). This number is what the user uses to manage each process (you can also use the process name). There are several commands to check the status of a running process. Take a quick look at these commands.
ps
The most common is the ps command. The default ps output is a simple list of running processes in the current Terminal, as follows, the first column contains the pid.
alan@workstation:~ $ps pid tty time cmd 23989 pts/0 00:00:00 bash 24148 pts/0 00:00:00 ps
For example, you would like to see the nginx process started earlier. To do this, you need to tell ps to show yourself all running processes (-e) and a complete 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 almost 300 lines, but has been shortened in this example. As you can imagine, trying to handle 300 lines of progress information is a bit difficult. You can convert this output to grep and filter it to display only nginx.
alan@workstation:~ $ps -efgrep 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
You can quickly see that the pid of nginx is 20520 and 20521.
pgrep
The pgrep command simplifies the problems encountered by calling grep alone.
alan@workstation:~ $pgrep nginx 20520 20521
Assuming you are in a hosted environment, many users are running several different versions of nginx. You can use the -u option to exclude others from output.
alan@workstation:~ $pgrep -u alan nginx 20520 20521
pidof
Another useful command is pidof. This will check the pid of a particular binary, even if another process with the same name is running. To build an example, I copied my nginx into the second directory and started with the corresponding path prefix. In fact, this example could be in a different location, such as folders owned by different users. If running two versions of nginx, the pidof output shows all processes.
alan@workstation:~ $ps -efgrep nginx alan 20881 1454 0 11:18?00:00:00 nginx:master process ./nginx -p/home/alan/web/prod/nginxsec alan 20882 20881 0 11:18?00:00:00 nginx:worker process alan 20895 1454 0 11:19?00:00:00 nginx:master process nginx alan 20896 20895 0 11:19?00:00:00 nginx:worker process
Using grep or pgrep will display the pid number, but you may not know which version it is.
alan@workstation:~ $pgrep nginx 20881 20882 20895 20896
The pidof command can be used to identify the pid of a particular version of nginx.
alan@workstation:~ $pidof/home/alan/web/prod/nginxsec/sbin/nginx 20882 20881 alan@workstation:~ $pidof/home/alan/web/prod/nginx/sbin/nginx 20896 20895
top
The top command has a long history, which is useful to see the details of running processes and to quickly identify problems like memory consumption. Its default view is shown below.
top-11:56:28 up 1 day, 13:37, 1 user, load average:0.09, 0.04, 0.03 tasks:292 total, 3 running, 225 sleeping, 0 stopped, 0 zombie %cpu (s):0.1 us, 0.2 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st kib mem:16387132 total, 10854648 free, 1859036 used, 3673448 buff/cache kib swap:0 total, 0 free, 0 used. 14176540 avail mem pid user pr ni virt res shr s%cpu%mem time + command 17270 alan 20 0 3930764 247288 98992 r 0.7 1.5 5:58.22 gnome-shell 20496 alan 20 0 816144 45416 29844 s 0.5 0.3 0:22.16 gnome-terminal- 21110 alan 20 0 41940 3988 3188 r 0.1 0.0 0:00.17 top 1 root 20 0 225564 9416 6768 s 0.0 0.1 0:10.72 systemd 2 root 20 0 0 0 0 s 0.0 0.0 0:00.01 kthreadd 4 root 0 -20 0 0 0 i 0.0 0.0 0:00.00 kworker/0:0h 6 root 0 -20 0 0 0 i 0.0 0.0 0:00.00 mm_percpu_wq 7 root 20 0 0 0 0 s 0.0 0.0 0:00.08 ksoftirqd/0
You can change the update interval by entering the s and preferred update seconds. To easily track sample nginx progress, for example, you can use the -p option and switch pid to call top. This output is much cleaner.
alan@workstation:~ $top -p20881 -p20882 -p20895 -p20896 tasks:4 total, 0 running, 4 sleeping, 0 stopped, 0 zombie %cpu (s):2.8 us, 1.3 sy, 0.0 ni, 95.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st kib mem:16387132 total, 10856008 free, 1857648 used, 3673476 buff/cache kib swap:0 total, 0 free, 0 used. 14177928 avail mem pid user pr ni virt res shr s%cpu%mem time + command 20881 alan 20 0 12016 348 0 s 0.0 0.0 0:00.00 nginx 20882 alan 20 0 12460 1644 932 s 0.0 0.0 0:00.00 nginx 20895 alan 20 0 12016 352 0 s 0.0 0.0 0:00.00 nginx 20896 alan 20 0 12460 1628 912 s 0.0 0.0 0:00.00 nginx
During process management, especially at the end of a process, it is important to correctly identify the pid. Also, if top is used in this way, whenever one of these processes stops or a new one starts, top needs to be notified of new processes.
End the process
kill
Interestingly, there is no stop order. In Linux, there is only the kill command. The kill command is used to send a signal to a process. The most common signals are "sigterm" or "sigkill". However, there is more to it than that. Following are some examples, the full list can be displayed with kill -l .
1) sighup 2) sigint 3) sigquit 4) sigill 5) sigtrap 6) sigabrt 7) sigbus 8) sigfpe 9) sigkill 10) sigusr1 11) sigsegv 12) sigusr2 13) sigpipe 14) sigalrm 15) sigterm
Note the 9th signal is sigkill. Usually you would send something like kill -9 20896 . The default signal is 15 - sigterm . Remember that many applications have their own stop methods. Nginx uses the -s option to switch signals, such as stop or reload. In general, most people prefer to use the application-specific method to stop working, however, I will demonstrate using the kill command to stop the nginx 20896 process, then use pgrep to verify receive that it has stopped. Pid 20896 no longer appears.
alan@workstation:~ $kill -9 20896 alan@workstation:~ $pgrep nginx 20881 20882 20895 22123
pkill
The pkill command is similar to pgrep in that it can search by name, which means you have to be very careful when using pkill. In this example nginx, if you only want to stop one instance of nginx, you probably won't choose to use pkill. You can either pass the nginx -s stop option to a specific instance to get rid of it, or use grep to filter the entire ps output.
/home/alan/web/prod/nginx/sbin/nginx -s stop /home/alan/web/prod/nginxsec/sbin/nginx -s stop
If you want to use pkill, you can include the -f option to allow pkill to filter the entire command line argument. This of course also applies to pgrep. So, when executing pkill -f , you can use pgrep -a first . Confirm it.
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
You can also use pgrep -f to narrow your results. pkill stops processing with the same parameters.
alan@workstation:~ $pgrep -f nginxsec 20881 alan@workstation:~ $pkill -f nginxsec
The important thing to remember about pgrep (especially pkill) is that you should always make sure that your search results are accurate. This way you won't inadvertently compromise the inaccuracy.
Most of these commands have multiple options, so you should read the man page for each command. Although most of these commands exist on platforms like linux, solaris, and bsd, there are some differences. When working on the command line or writing scripts, always test them and be ready to fix them when needed.
You should read it
- Use the Top command to see the processes running in Linux
- How to manage the priority of I / O process in Linux
- Instructions for using pstree command on Linux
- 4 best download managers for Linux users
- What is Display Manager on Linux?
- How to manage Linux services using Systemd
- How to use the which command in Linux
- 7 Task Manager processes that should never end
- Decode how the package manager works and software installation on Linux
- 14 interesting Linux commands in Terminal
- How to use winget - New Package Manager for Windows 10
- 8 best Linux password managers
Maybe you are interested
LG EXAONE launched: Korea's first open source AI model How to find broken symlinks in Linux How to change the background image for the Sidecar screen on iPad Asus launches a gaming computer with ROG line Offering free license for HD Video Converter Factory Pro, video conversion software costs USD 49.95 List of common shortcuts for Google Sheets on Chrome OS (Part 3)