Linux-common process operation commands
1. Process Monitoring command (ps ):
To monitor and control processes, you must first understand the current process, that is, you need to view the current process, and the ps command is the most basic and very powerful process viewing command. You can use this command to determine which processes are running and running, whether the process is terminated, whether the process is dead, and which processes are occupying excessive resources. In short, most of the information can be obtained by executing this command.
Ps commands have many command line options and parameters. However, we usually only use the following two methods:
Option description
A displays all processes on the terminal, including those of other users.
U displays the program status in user-based format.
X shows all programs, which are not distinguished by terminals.
-E: displays all processes.
O and then specify the columns to be output, such as user and pid. Multiple columns are separated by commas.
-P is followed by a list of pid IDs separated by commas. This command will only output the relevant data of these PIDs.
/> Ps aux
Root 1 0.0 0.1 2828 1400? Ss/sbin/init
Root 2 0.0 0.0 0 0? S [kthreadd]
Root 3 0.0 0.0 0 0? S [migration/0]
......
/> Ps-eo user, pid, % cpu, % mem, start, time, command | head-n 4
User pid % CPU % MEM STARTED TIME COMMAND
Root 1 0.0 0.1 09:51:08/sbin/init
Root 2 0.0 0.0 09:51:08 00:00:00 [kthreadd]
Root 3 0.0 0.0 09:51:08 [migration/0]
It should be noted that there are many parameters related to process performance in ps, which are displayed as columns in the output table, here we only provide several frequently used parameters. For more parameters, we need to go to the ps man manual based on the actual situation of our application.
# Display the data of processes whose pid is 1 (init) in the complete format
/> Ps-fp 1
UID PID PPID C STIME TTY TIME CMD
Root 1 0 0 05:16? 00:00:03/sbin/init
2. commands for changing the process priority (nice and renice ):
The most common usage of this Shell command is nice [-n <priority level>] [Execute Command]. The priority level ranges from-20 to 19, and-20 is the highest, 19 is the lowest. Only the system administrator can set a negative number.
# Run sleep in the background for 100 seconds and set its nice value to 19 at startup
/> Nice-n 19 sleep 100 &
[1] 4661
# Run sleep in the background for 100 seconds and set its nice value to-19 at startup.
/> Nice-n-19 sleep 100 &
[2] 4664
# Follow the two lines highlighted in yellow in the ps-l output. Their NI values are consistent with the set values.
/> Ps-l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 0 2833 2829 0 80 0-1739-pts/2 00:00:00 bash
0 S 0 4661 2833 0 99 19-1066-pts/2 00:00:00 sleep
4 S 0 4664 2833 0 61-19-1066-pts/2 00:00:00 sleep
4 R 0 4665 2833 1 80 0-1231-pts/2 00:00:00 ps
The renice command is used to reset the nice value for the executed process. The command contains the following common options:
Option description
-G: Use the program group name to modify the priority of all programs affiliated with the program group.
-P changes the priority level of the program. This parameter is a preset value.
-U specifies the user name to modify the priority of all programs belonging to the user.
# Switch to the stephen user and execute a background process. Here, the sleep process will sleep for 1000 seconds in the background.
/> Su stephen
/> Sleep 1000 &
[1] 4812
/> Exit # Return to the root user before switch
# View the started background sleep process. The ni value is 0 and the host user is stephen.
/> Ps-eo user, pid, ni, command | grep stephen
Stephen 4812 0 sleep 1000
Root 4821 0 grep stephen
# Modify the nice value of all processes of a specified user
/> Renice-n 5-u stephen
500: old priority 0, new priority 5
# From the output result of executing ps again, we can see that the nice value of the sleep background process has been adjusted to 5
/> Ps-eo user, pid, ni, command | grep stephen
Stephen 4812 5 sleep 1000
Root 4826 0 grep stephen
# Modify the nice value of a process by specifying the process pid
/> Renice-n 10-p 4812
4812: old priority 5, new priority 10
# Execute ps again. The nice value of the sleep background process has changed from 5 to 10.
/> Ps-eo user, pid, ni, command | grep stephen
Stephen 4812 10 sleep 1000
Root 4829 0 grep stephen
For more details, please continue to read the highlights on the next page: