--pid文件作用
让程序快速读取进程信息,用于kill进程,检测进程是否存在等
查看进程信息:ll /proc/8043
----ps----
--查看内存占用(进程)---
ps -e -o 'pid,comm,args,pcpu,rsz,vsz,stime,user,uid'|sort -nrk5 #rsz是是实际内存
ps axu|head -1;ps aux | grep -v '\]'|sort -nrk6 #VSZ: 占用的虚拟记忆体大小 RSS: 占用的记忆体大小
--查看完整命令:
ps -f -w -p 4651
-----通过端口过滤进程号------
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}'
#检测进程是否存在
killall -0 httpd
ps -C nginx --no-header
---分析进程调用pstack和starce----
pstack
pstack用来跟踪进程栈,这个命令在排查进程问题时非常有用,比如我们发现一个服务一直处于work状态(如假死状态,好似死循环),使用这个命令就能轻松定位问题所在;可以在一段时间内,多执行几次pstack,若发现代码栈总是停在同一个位置,那个位置就需要重点关注,很可能就是出问题的地方;
示例:查看bash程序进程栈:
]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常用来跟踪进程执行时的系统调用和所接收的信号。 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须由用户态模式切换至内核态模式,通过系统调用访问硬件设备。strace可以跟踪到一个进程产生的系统调用,包括参数,返回值,执行消耗的时间。
示例:
$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)
...
每一行都是一条系统调用,等号左边是系统调用的函数名及其参数,右边是该调用的返回值。 strace 显示这些调用的参数并返回符号形式的值。strace 从内核接收信息,而且不需要以任何特殊的方式来构建内核。
跟踪可执行程序
strace -f -F -o ~/straceout.txt myserver
-f -F选项告诉strace同时跟踪fork和vfork出来的进程,-o选项把所有strace输出写到~/straceout.txt里 面,myserver是要启动和调试的程序。
跟踪服务程序
strace -o output.txt -T -tt -e trace=all -p 45970
跟踪28979进程的所有系统调用(-e trace=all),并统计系统调用的花费时间,以及开始时间(并以可视化的时分秒格式显示),最后将记录结果存在output.txt文件里面。
-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程序调试---
--jps
usage: jps [-help]
jps [-q] [-mlvV] [<hostid>]
Definitions:
<hostid>: <hostname>[:<port>]
-q 不输出类名、Jar名和传入main方法的参数
-m 输出传入main方法的参数
-l 输出main类或Jar的全限名
-v 输出传入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
还有一个很常用的情况是:用jmap把进程内存使用情况dump到文件中,再用jhat分析查看。jmap进行dump命令格式如下:
1,jmap -dump:format=b,file=/tmp/dump.dat 12760
2,使用jhat输出到浏览器:
jhat -port 9998 /tmp/dump.dat
3,然后就可以在浏览器中输入主机地址:9998查看了
--jstack
##执行命令需使用启动进程用户
jstack -l 28367 >jstack_28367.log
Usage:
jstack [-l] <pid>
(to connect to running process) 连接活动线程
jstack -F [-m] [-l] <pid>
(to connect to a hung process) 连接阻塞线程
jstack [-m] [-l] <executable> <core>
(to connect to a core file) 连接dump的文件
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) 不仅会输出Java堆栈信息,还会输出C/C++堆栈信息(比如Native方法)
-l long listing. Prints additional information about locks 会打印出额外的锁信息,在发生死锁时可以用jstack -l pid来观察锁持有情况
常用方式:
1,top -Hp 12760 #查看进程的线程信息
2,printf "%x\n" 12923 #找出线程,查看16进制
3,jstack 12760|grep 327b #grep进程堆栈信息
--jstat
jstat -gc 12760 250 4
字段解释:
S0C、S1C、S0U、S1U:Survivor 0/1区容量(Capacity)和使用量(Used)
EC、EU:Eden区容量和使用量
OC、OU:年老代容量和使用量
PC、PU:永久代容量和使用量
YGC、YGT:年轻代GC次数和GC耗时
FGC、FGCT:Full GC次数和Full GC耗时
GCT:GC总耗时
---top----
Usage:
top -hv | -bcHiOSs -d secs -n max -u|U user -p pid(s) -o field -w [cols]
-H :Threads-mode
快捷键:
command sorted-field supported
A start time (non-display) No
M %MEM Yes
N PID Yes
P %CPU Yes
T TIME+ Yes
例,查看进程的线程:
top -Hp 12760
---pstree----
格式: pstree [option] [ PID | USER ]
参数:
-a, --arguments show command line arguments
-p, --show-pids show PIDs; implies -c
-s, --show-parents show parents of the selected process
脚本内可以: pstree -pa $$
--查看进程env:
cat /proc/3319/environ |tr '\0' '\n'