introduce
The ps command is a commonly used process viewing tool in Linux systems. It can view information about processes running in the current system. The ps command can display the process's ID, status, executed commands, memory usage and other information. For system administrators, the ps command can easily monitor and manage processes in the system.
ps command syntax and options
The basic syntax of the ps command is as follows:
ps [选项]
Commonly used options include:
Options | illustrate |
---|---|
-a | Show all processes, including those of other users |
-u | Show process details |
-x | Show processes that do not have a controlling terminal |
-e | Show all processes in the system |
-f | Displays the full format of a process, including its command line arguments |
-l | Display detailed information about the process, including process status, resource usage, etc. |
-h | Hide title row |
for example
1. View all processes of the current user
Use ps
the command to view all processes of the current user. The command is as follows:
ps
This command will display all process information of the current user, as shown below:
PID TTY TIME CMD
302 tty7 00:10:29 Xorg
4372 tty1 00:00:01 bash
4603 pts/0 00:00:00 ps
In the output result, the first column is the ID of the process (PID), the second column is the control terminal (TTY) where the process is located, the third column is the running time of the process (TIME), and the fourth column is the command of the process (CMD). ).
2. Show all processes
Use the ps -A
or ps -e
command to display information about all processes in the system, including processes of other users. The effects of the two commands are the same, and the execution results are as follows:
PID TTY TIME CMD
1 ? 00:00:06 systemd
2 ? 00:00:00 kthreadd
3 ? 00:00:00 rcu_gp
4 ? 00:00:00 rcu_par_gp
…
3. Display process details
Use ps -ef
the command to display detailed information about the process, including process status, resource usage and other information. The execution results are as follows:
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 12月11 ? 00:00:06 /sbin/init splash
root 2 0 0 12月11 ? 00:00:00 [kthreadd]
root 3 2 0 12月11 ? 00:00:00 [rcu_gp]
…
4. Run a long task in the background
In Linux, sometimes you need to run a task in the background that takes a long time to complete. You can use nohup
commands and &
symbols to achieve this. The sample code is as follows:
nohup command > /dev/null 2>&1 &
Among them, command
means the command that needs to be run, >/dev/null 2>&1
means redirecting both standard output and standard error to /dev/null
, and &
the symbol means running the command in the background.
5. Find processes (commonly used)
In Linux, sometimes you need to find information about a process or kill a process. In this case, you can use ps
the command with grep
the command and kill
command to achieve this.
For example, assuming we want to find all nginx
process information containing the keyword , we can use the following command:
ps -ef | grep nginx
The execution results are as follows:
root 1608 1 0 11:40 ? 00:00:00 nginx: master process nginx -g daemon on; master_process on;
www-data 1610 1608 0 11:40 ? 00:00:00 nginx: worker process
www-data 1611 1608 0 11:40 ? 00:00:00 nginx: worker process
If you want to kill nginx
the main process with ID 1608, you can use the following command:
kill -9 1608
4. Real-time display of progress
Sometimes you need to check the status of a process in real time, you can use top
the command. After executing top
the command, the status, resource usage and other information of each process in the system will be displayed in real time, as shown below:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7583 root 20 0 319236 88264 17976 S 0.7 11.5 0:06.98 Xorg
7829 binjie09 20 0 1476780 361616 109568 S 0.7 47.3 2:36.70 chrome
7941 binjie09 20 0 1147772 249564 100064 S 0.7 32.6 0:40.26 gnome-shell
8334 binjie09 20 0 1072240 252328 116060 S 0.7 33.0 0:12.02 gnome-terminal-
8564 binjie09 20 0 607192 183952 75352 S 0.7 24.0 0:08.58 chrome
Use on the command line Ctrl+C
to exit top
the command.
5. Check the ports occupied by the process
Sometimes you need to check the port occupied by a certain process. You can use netstat
the command with grep
the command to achieve this. For example, if you want to find information about all 8080
processes occupying port number , you can use the following command:
netstat -nlp | grep 8080
The execution results are as follows:
tcp6 0 0 :::8080 :::* LISTEN 7829/chrome
Among them, "LISTEN" means that the port is listening for connection requests.
Summarize
The ps command is a commonly used process viewing tool in Linux systems. It can view process information currently running in the system and can easily monitor and manage processes in the system. The options of the ps command are rich and diverse, and the usage method is flexible. You can select different options to view according to actual needs. In daily work, it is very important to be proficient in the use of ps commands, and it will also help improve work efficiency.