ps command

As a derivative operating system of Unix, Linux has a built-in tool ps to view the current process. This tool can be used from the command line.

What is the PS command

Looking at its man page, you can see that the ps command can give a snapshot of the processes in the current system. It captures the process state of the system at a certain event. If you want to continuously update the status of the view, you can use the top command.

The ps command supports three syntax formats used

    UNIX style, options can be combined together, and options must be preceded by a "-" hyphen
    BSD style, options can be grouped together, but options cannot be preceded by a "-" hyphen
    GNU style Long options with two "-" hyphens before options

We can mix these styles, but conflicts may arise. This article uses the UNIX-style ps command. Here are examples of ps commands that are used more in daily life.
1. Execute the ps command without parameters

This is a basic ps usage. Execute this command in the console and see the result.

Execute the ps command

without options

. The result will display 4 columns of information by default.

    PID: the process number of the running command (CMD)
    TTY: the location where the command is running (terminal)
    TIME: the CPU processing time occupied by
    the running command CMD: the command running by the process

This information is displayed unsorted .
2. Show all current processes

Use the -a parameter. -a stands for all. Adding the x parameter at the same time will show processes that do not have a controlling terminal.

    The result of the command $ ps -ax

may be very long. For ease of viewing, it can be used in combination with the less command and a pipeline.

    $ ps -ax | less

ps all info

ps all info
3. Filter processes by user

In the case where we need to view specific user processes, we can use the -u parameter. For example, if we want to view the processes of user 'pungki', we can use the following command:

    $ ps -u pungki

filter by user filter

by user
4. Filter processes by cpu and memory usage

Maybe the results by CPU or memory usage , so you can find which process is consuming your resources. To do this, we can use the aux parameter to display comprehensive information:

    $ ps -aux | less

show comprehensive information

show comprehensive information

When the results are long, we can use pipes and the less command to filter.

The default result set is unsorted. It can be sorted by the --sort command.

Sort ascending based on CPU usage

    $ ps -aux --sort -pcpu | less

Sort based on cpu usage Sort ascending

based on cpu usage Sort

ascending based on memory usage

    $ ps -aux --sort -pmem | less

Sort based on memory usage

Sort by memory usage

We can also combine them into one command and pipe the top 10 results:

    $ ps -aux --sort -pcpu,+pmem | head -n 10

5. Filter by process name and PID

use - C parameter, followed by the name of the process you are looking for. For example, to display information about a process named getty, we can use the following command:

    $ ps -C getty

filter by process name and PID filter

by process name and PID

If we want to see more details, we can use the -f parameter To see a formatted list of messages:

    $ ps -f -C getty

filter by process name and PID filter

by process name and PID
6. Filter processes by thread

If we want to know the thread of a specific process, we can use the -L parameter, followed by specific PID.

    $ ps -L 1213

Filter processes based on threads Filter processes

based on threads
7. Display processes in tree form

Sometimes we want to display processes in a tree structure, you can use the -axjf parameter.

    $ps -axjf

show process

tree view

or you can use another command.

    $ pstree

tree display process

tree display process
8. Show Security Information

If you want to see who is currently logged into your server. You can use the ps command with related parameters:

    $ ps -eo pid,user,args

parameter -e displays all process information, -o parameter controls the output. The Pid, ​​User and Args parameters show the PID, the user running the application and the application.

Display Security Information

Display Security Information

The keywords that can be used with the -e parameter are args, cmd, comm, command, fname, ucmd, ucomm, lstart, bsdstart, and start.
9. Format output of processes created by root user (real or effective UID) When a

system administrator wants to view processes run by root user and other related information about this process, they can use the following command:

    $ ps -U root -u root u

-U parameter filters processes by real user ID (RUID), it selects real user name or ID from user list. The real user is the user who actually created the process.

The -u parameter is used to filter for effective user IDs (EUIDs).

The final u parameter is used to determine the output in user-specific format, consisting of the columns User, PID, %CPU, %MEM, VSZ, RSS, TTY, STAT, START, TIME and COMMAND.

Here is the output of the above command:

show real and effective User ID

show real and effective User ID
10. Use PS to monitor process status in real time

The ps command will display the current process status of your system, but the result is static.

When there is a situation, we need to filter processes by CPU and memory usage as mentioned in point 4 above, and we want the results to be refreshed every second. For this, we can combine the ps command and the watch command.

    $ watch -n 1 'ps -aux --sort -pmem, -pcpu'

Combining ps and watch

Combining ps and watch

If the output is too long, we can also limit it, such as the first 20, we can use the head command to do it .

    $ watch -n 1 'ps -aux --sort -pmem, -pcpu | head 20'

Combining ps and watch

Combining ps and watch

The dynamic viewing here is not like the top or htop commands. But the advantage of using ps is that you can define the fields that are displayed, and you can choose the fields you want to view.

For example, if you only want to see information about the user named 'pungki', you can use the following command:

    $ watch -n 1 'ps -aux -U pungki u --sort -pmem, -pcpu | head 20'

Combining ps and watch

Combining ps and watch
Conclusion

You probably use the ps command every day to monitor your Linux system. But in fact, you can generate all kinds of reports you need through the parameters of the ps command.

Another advantage of the ps command is that ps is installed by default on various Linux systems, so you only need to use it.

Don't forget to check more parameters with man ps. (LCTT Annotation: Because the ps command is old and important, its parameters are different in different UNIX, BSD, Linux, etc. systems, so if you are not using a Linux system, please refer to your documentation for specific available parameters. )




Record your commonly used linux system commands for future reference, and find that the memory is getting worse and worse.
Find the java thread
ps command that consumes the most CPU

Command : ps -mp pid -o THREAD,tid,time or ps -Lfp pid

result display :



The function of this command is to obtain some information about the thread corresponding to a process. For example, if you want to analyze some running bottlenecks of a java process, you can use this command to find the CPU time of all current threads, which is the last column here.



For example, a TID: 30834 is found here, and the TIME occupied is the highest.

First convert it into hexadecimal through printf "%x\n" 30834, and continue to dump the stack information of the current jvm process through the jstack command. Through the Grep command, you can find the thread ID information corresponding to hexadecimal, and you can quickly find out where the code corresponding to the most CPU consumption is.

Under a simple explanation, the content of this string of thread information under jstack:


Java code Collection code

    "DboServiceProcessor-4-thread-295" daemon prio=10 tid=0x00002aab047a9800 nid=0x7d9b waiting on condition [0x0000000046f66000] 

nid : corresponding linux operating system The tid below is the hexadecimal number converted earlier

tid: This should be the only address location in the jmm memory specification of the jvm. If you use it when you analyze some memory data of the jvm in detail, I have not reached that level myself, so put down the
top command first

Command : top -Hp pid

The results show that:



with the previous effect, you can track and obtain the most CPU-consuming threads in the specified process in real time. Then use the previous method to extract the corresponding thread stack information.



Determine the I/O bottleneck
mpstat command

Command : mpstat -P ALL 1 1000

The result shows:



pay attention to the %iowait column here, the time spent by the CPU waiting for I/O operations. A persistently high value can usually be caused by an I/O bottleneck.

Through this parameter, you can intuitively see whether the current I/O operation has a bottleneck


iostat command

command : iostat -m -x 1 1000




You can also observe the %iowait data in the corresponding CPU. In addition, iostat also provides Some more detailed I/O status data, such as the more important ones:

avgqu-sz : The average queue length of the requests that were issued to the device. (The request length of the disk queue, 2,3 is better if it is normal. Yes Same understanding as cpu load)

await : The average time (in milliseconds) for I/O requests issued to the device to be served. (represents the total time from wait to completion of an I/O operation)

Both svctm and %util represent the ratio of the time spent processing the I/O request and the CPU time. When judging whether it is a bottleneck, these two parameters are not the main



r/sw/s and rMB/s wMB/s are some states of the I/O processed by the current system. The former is what we often call tps, and the latter is throughput. quantity. This is also a pid command for evaluating a system's performance indicators Command


: pidstat -p pid -u -d -t -w -h 1 1000 The result shows: a very practical command, which can analyze the corresponding performance data based on the current process, including CPU , I/O, IR, CS, etc., which can facilitate developers to observe the running status of the system in a more refined manner. However, pidstat seems to be available in some newer versions of the 2.6 kernel, and the sysstat package needs to be installed. Under ubuntu, it can be installed by sudo apt-get install sysstat. sar command Command : sar -x pid 1 1000 sar can also specify the corresponding pid and pay attention to several fixed parameters, which is not as powerful as pidstat. Can not see the corresponding I/O, IR and other information. The functions of sar can cover the related functions of mpstat and iostat. dstat command Command : dstat -y --tcp 1 1000 Through dstat --tcp, you can easily see the various states of the current tcp, you don't need to see netstat -nat every time. Other commands netstat -natp : view the corresponding network Link, pay attention to Recv-Q , Send-Q , State.




































lsof -p pid : Find the file handle of the corresponding pid

lsof -i : 80 : Find which process the corresponding port is occupied

lsof /tmp/1.txt : Find which process the corresponding file is occupied



tcpdump / wireshark : Packet capture analysis tool

jstat / jmap / jstack / jps and a series of java monitoring commands


Finally

  , if you want to do some performance tuning work, you must be good at using some tools to pay attention to the corresponding status. Through linux commands, you can easily observe some peripheral states such as CPU, I/O, network, etc., and most of the problems can be solved in many cases. Some running status monitoring inside the JVM requires fine-grained observation with the help of some unique tools.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326273383&siteId=291194637