Linux ps command
Linux's ps command is a basic tool for viewing processes run by the system. It provides a snapshot of the current process with specific information, such as the user ID, cpu usage, memory usage, and command name. It does not display data in real time like top or htop. Although it is simpler in terms of functions and outputs, it is still a necessary process management/detection tool for every linux beginner to understand and learn well.
In this article, we will learn the basic usage of ps commands: searching, filtering, and sorting in different ways.
Syntax description
Ps commands have two syntax rules: BSD and UNIX. Linux beginners are often confused and will misunderstand these two styles. Therefore, before proceeding to the next step, let's find some basic information.
Note: "ps aux" is not the same as "ps-aux ". For example, "-u" is used to display User processes, but "u" means to display specific information.
BSD format-BSD syntax options do not have a break, such:
ps aux
UNIX/LINUX format-there is a break before the option of the syntax in linux format, such:
ps -ef
It is possible to mix these two syntaxes in linux. For example, "ps ax-f ". However, in this chapter, we mainly discuss the UNIX format syntax.
How to Use the ps command 1. display all processes
The following command displays a list of all processes.
$ ps ax$ ps -ef
Output to "less" Through MPs queues can be paged.
You can use the "u" or "-f" option to display the process details.
$ ps aux$ ps -ef -f
Why is my USER Name Not displayed in the USER column, but others such as root and www-data are displayed? For all users (including yours), if the length is greater than 8 characters, the ps will only display your UID rather than the user name.
2. Display User processes
Use the "-u" option followed by the user name to filter processes of the user. Multiple user names can be separated by commas.
$ ps -f -u www-dataUID PID PPID C STIME TTY TIME CMDwww-data 1329 1328 0 09:32 ? 00:00:00 nginx: worker processwww-data 1330 1328 0 09:32 ? 00:00:00 nginx: worker processwww-data 1332 1328 0 09:32 ? 00:00:00 nginx: worker processwww-data 1377 1372 0 09:32 ? 00:00:00 php-fpm: pool a.localhost www-data 1378 1372 0 09:32 ? 00:00:00 php-fpm: pool a.localhost www-data 4524 2359 0 10:03 ? 00:00:00 /usr/sbin/apache2 -k startwww-data 4527 2359 0 10:03 ? 00:00:00 /usr/sbin/apache2 -k startwww-data 4528 2359 0 10:03 ? 00:00:00 /usr/sbin/apache2 -k start
3. display the process by name or process id
You can search for a process by adding a name or command after the "-C" option.
$ ps -C apache2 PID TTY TIME CMD 2359 ? 00:00:00 apache2 4524 ? 00:00:00 apache2 4525 ? 00:00:00 apache2...
To display a process by process id, use the "-p" option and use commas to separate multiple process IDs.
$ ps -f -p 3150,7298,6544
"-C" must provide a precise process name, and it cannot be searched by some names or wildcards. To search for the process list more flexibly, The grep command is usually used.
$ ps -ef | grep apache
4. Sort processes by cpu or memory usage
The system administrator usually wants to find out the processes that consume the most memory or CPU. The sorting option sorts the process list based on specific fields or parameters.
You can use '-- sort' to specify multiple fields and use commas to separate them. In addition, fields can be prefixed with '-' or '+' to indicate descending and ascending sorting. There are many options for sorting. You can use the man page to obtain the complete list.
$ ps aux --sort=-pcpu,+pmem
Displays the top five cpu-consuming processes.
$ ps aux --sort=-pcpu | head -5USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 1 2.6 0.7 51396 7644 ? Ss 02:02 0:03 /usr/lib/systemd/systemd --switched-root --system --deserialize 23root 1249 2.6 3.0 355800 30896 tty1 Rsl+ 02:02 0:02 /usr/bin/X -background none :0 vt01 -nolisten tcproot 508 2.4 1.6 248488 16776 ? Ss 02:02 0:03 /usr/bin/python /usr/sbin/firewalld --noforksilver 1525 2.1 2.3 448568 24392 ? S 02:03 0:01 /usr/bin/python /usr/share/system-config-printer/applet.py
For more details, please continue to read the highlights on the next page:
Recommended reading:
Linux traffic monitoring tool-iftop
Linux top commands
Top commands in Linux
Efficient use of top commands in Linux
Linux top commands
Linux System Monitoring load top Command details