Notes-linux commands (3 process related)

             --pid file function
                allows the program to quickly read process information, used to kill the process, check whether the process exists, etc.

                View process information: ll /proc/8043

             ----ps----                     --View
                memory usage (process)---
ps -e -o'pid,comm,args,pcpu,rsz,vsz,stime,user,uid'|sort -nrk5 #rsz Yes is the actual memory
                    ps axu|head -1;ps aux | grep -v'\]'|sort -nrk6 #VSZ: virtual memory size used RSS: memory size used
                    
                --see the complete command:
                    ps -f -w -p 4651
            
                -----filter process number by port------
                    ss -anp|grep -w 31140|awk'{print $NF}'|awk -F,'{print $2}'| awk -F ='{print $NF}'
                    netstat -anp|grep -w 31140|awk'{print $NF}'|awk -F/'{print $1}'             #Detect
                    
                whether the process exists
                    killall -0 httpd
                    ps -C nginx --no-header
        
---Analyze the process calls pstack and starce----
                pstack
                    pstack is used to track the process stack. This command is very useful when troubleshooting process problems. For example, we find that a service has been in the work state (such as suspended animation, like an infinite loop). Using this command, you can easily locate the problem; If you find that the code stack always stops at the same location within a period of time, execute pstack several times, and that location needs to be focused on, which is likely to be the problem;

                    Example: View the bash program process stack:
                    ]ps -ef|grep bash
                    root 3094 1 0 Jan07? 00:00:40 /bin/bash /usr/sbin/ksmtuned
                    root 35569 61796 0 10:14 pts/2 00:00: 00 grep --color=auto bash
                    root 61796 61754 0 Jan25 pts/2 00:00:00 -bash
                    root 88316 88308 0 Jan21 pts/1 00:00:00 -bash
                    ]pstack 61796
                    #0 0x00007f34524f227c in waitpid () from / lib64/libc.so.6
                    #1 0x0000000000440904 in waitchld.isra.10 ()
                    #2 0x0000000000441bbc in wait_for ()
                    #3  0x000000000043392e in execute_command_internal ()
                    #4  0x0000000000433b5e in execute_command ()
                    #5  0x000000000041e285 in reader_loop ()
                    #6  0x000000000041c8ee in main ()

                strace
                    strace is often used to track system calls and signals received during process execution. In the Linux world, processes cannot directly access hardware devices. When processes need to access hardware devices (such as reading disk files, receiving network data, etc.), they must switch from user mode to kernel mode, and access hardware devices through system calls. strace can track the system calls generated by a process, including parameters, return values, and execution time.
                    Example:

                    $strace cat /dev/null
                    execve("/bin/cat", ["cat", "/dev/null"], [/* 22 vars */]) = 0
                    brk(0) = 0xab1000
                    access("/ etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
                    mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f29379a7000
                    access("/etc/ld .so.preload", R_OK) = -1 ENOENT (No such file or directory)
                    ...
                    Each line is a system call, the left side of the equal sign is the name of the system call and its parameters, and the right side is the return value of the call . strace displays the parameters of these calls and returns the value in symbolic form. strace receives information from the kernel and does not need to build the kernel in any special way.

                    Tracking executable programs

                    strace -f -F -o ~/straceout.txt myserver
                    -f -F option tells strace to track the processes from fork and vfork at the same time, -o option writes all strace output to ~/straceout.txt, myserver is to start and The debugged program.

                    The tracing service program
                    strace -o output.txt -T -tt -e trace=all -p 45970
                    traces all system calls of the 28979 process (-e trace=all), and counts the time spent on system calls and the start time (with Visualized hour, minute and second format display), and finally save the recorded result in the output.txt file.
                    
                    -f - follow forks, -ff - with output into separate files
                    -T - print time spent in each syscall
                    -e expr - a qualifying expression: option=[!]all or option=[!]val1[, val2]...
                       options: trace, abbrev, verbose, raw, signal, read, write
                    -o file - send trace output to FILE instead of stderr
                    -p pid - trace process with process id PID, may be repeated
                    
            - java program debugging---
                --jps
                    usage: jps [-help]
                           jps [-q] [-mlvV] [<hostid>]

                    Definitions:
                        <hostid>: <hostname>[:<port>]
                        -q does not output the class name, Jar name and the parameters
                        passed into the main method -m outputs the parameters passed into the main method
                        -l outputs the fully qualified name of the main class or Jar
                        -v Output the parameters passed into the JVM
                        
                --jmap
                    Usage:
                        jmap [option] <pid>
                            (to connect to running process)
                        jmap [option] <executable <core>
                            (to connect to a core file)
                        jmap [option] [server_id @]<remote server IP or hostname>
                            (to connect to remote debug server)

                    where <option> is one of:
                        <none>               to print same info as Solaris pmap
                        -heap                to print java heap summary
                        -histo[:live]        to print histogram of java object heap; if the "live"
                                             suboption is specified, only count live objects
                        -clstats             to print class loader statistics
                        -finalizerinfo       to print information on objects awaiting finalization
                        -dump:<dump-options> to dump java heap in hprof binary format
                                             dump-options:
                                               live         dump only live objects; if not specified,
                                                            all objects in the heap are dumped.
                                               format=b     binary format
                                               file=<file>  dump heap to <file>
                                             Example: jmap -dump:live,format=b,file=heap.bin <pid>
                        -F                   force. Use with -dump:<dump-options> <pid> or -histo
                                             to force a heap dump or histogram when <pid> does not
                                             respond. The "live" suboption is not supported
                                             in this mode.
                        -h | -help           to print this help message
                        -J<flag>             to pass <flag> directly to the runtime system

                        There is also a very common situation: use jmap to dump the process memory usage to a file, and then use jhat to analyze and view. The format of the dump command by jmap is as follows:
                            1. jump -dump:format=b,file=/tmp/dump.dat 12760
                            2. Use jhat to output to the browser:
                                jhat -port 9998 /tmp/dump.dat
                            3, then you can Enter the host address in the browser: 9998 Viewed
                                
                --jstack
                    ##Execute the command to use the startup process user
                    jstack -l 28367 >jstack_28367.log
                    Usage:
                        jstack [-l] <pid>
                            (to connect to running process) Connection activity Thread
                        jstack -F [-m] [-l] <pid>
                            (to connect to a hung process) connection blocking thread
                        jstack [-m] [-l] <executable> <core>
                            (to connect to a core file)
                        jstack [-m] [-l] [server_id@]<remote server IP or hostname>
                            (to connect to a remote debug server)
                    Options:
                        -F to force a thread dump. Use when jstack <pid> does not respond (process is hung)
                        -m to print both java and native frames (mixed mode) Not only will it output Java Stack information, also output C/C++ stack information (such as Native method)
                        -l long listing. Prints additional information about locks will print additional lock information, you can use jstack -l pid to observe the lock holding when a deadlock occurs The
                    common way of the situation :
                        1, top -Hp 12760 #View the thread information of the process
                        2, printf "%x\n" 12923 #Find the thread, view the hexadecimal
                        3, jstack 12760|grep 327b #grep process stack information
                        
                --jstat
                    jstat -gc 12760  250 4
                    field explanation:
                        S0C, S1C, S0U, S1U: Survivor 0/1 zone capacity (Capacity) and usage (Used)
                        EC, EU: Eden zone capacity and usage
                        OC, OU: Old generation capacity and usage
                        PC , PU: permanent generation capacity and usage
                        YGC, YGT: young generation GC times and GC time consumption
                        FGC, FGCT: Full GC times and Full GC time consumption
                        GCT: Total GC time consumption
                    
            ---top----
                Usage:
                    top -hv | -bcHiOSs -d secs -n max -u|U user -p pid(s) -o field -w [cols]
                        -H :Threads-mode
                Shortcut key:
                    command sorted-field supported
                    A start time (non -display) No
                    M %MEM Yes
                    N PID Yes
                    P %CPU Yes
                    T TIME+ Yes For
                example, check the threads of the process:
                    top -Hp 12760

            ---pstree----
                Format: pstree [option] [PID | USER]
                Parameters:
                    -a, --arguments show command line arguments
                    -p, --show-pids show PIDs; implies -c
                    -s, - The show-parents show parents of the selected process
                script can be: pstree -pa $$ --view
                
            process env:
                    cat /proc/3319/environ |tr'\0''\n'

Guess you like

Origin blog.csdn.net/weixin_42573277/article/details/113928168