Strace 的初步使用, 结合nginx
一,安装
sudo apt-get install strace
测试中可能用到的命令
ps -ef | grep nginx
ps -eo pid,ppid,sid,tty,pgrp,comm | grep -E 'bash|PID|nginx'
二,使用
我首先进入到nginx目录下
cwl@cwl-PC:/usr/local/nginx$ pwd
/usr/local/nginx
cwl@cwl-PC:/usr/local/nginx$ ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
启动nginx
我们启动nginx, 拉起了四个进程
cwl@cwl-PC:/usr/local/nginx/sbin$ sudo ./nginx
cwl@cwl-PC:/usr/local/nginx/sbin$ ps -ef | grep nginx
root 9761 1 0 19:55 ? 00:00:00 nginx: master process ./nginx
nobody 9762 9761 0 19:55 ? 00:00:00 nginx: worker process
nobody 9763 9761 0 19:55 ? 00:00:00 nginx: worker process
nobody 9764 9761 0 19:55 ? 00:00:00 nginx: worker process
nobody 9765 9761 0 19:55 ? 00:00:00 nginx: worker process
cwl 9778 15969 0 19:56 pts/0 00:00:00 grep nginx
cwl@cwl-PC:/usr/local/nginx/sbin$ ps -eo pid,ppid,sid,tty,pgrp,comm | grep -E 'bash|PID|nginx'
PID PPID SID TT PGRP COMMAND
9761 1 9761 ? 9761 nginx
9762 9761 9761 ? 9761 nginx
9763 9761 9761 ? 9761 nginx
9764 9761 9761 ? 9761 nginx
9765 9761 9761 ? 9761 nginx
11165 11135 11165 pts/2 11165 bash
15969 15947 15969 pts/0 15969 bash
16497 16476 16497 pts/1 16497 bash
strace追踪进程信号
命令格式
sudo strace -e trace=signal -p {pid}
终端一,贴上strace
cwl@cwl-PC:~$ sudo strace -e trace=signal -p 9761
[sudo] cwl 的密码:
strace: Process 9761 attached
rt_sigsuspend([], 8
终端二,断开nginx
cwl@cwl-PC:/usr/local/nginx/sbin$ sudo ./nginx -s quit
终端一,接收到信号
strace: Process 9761 attached
rt_sigsuspend([], 8) = ? ERESTARTNOHAND (To be restarted if no handler)
--- SIGQUIT {si_signo=SIGQUIT, si_code=SI_USER, si_pid=12201, si_uid=0} ---
rt_sigreturn({mask=[HUP INT QUIT USR1 USR2 ALRM TERM CHLD WINCH IO]}) = -1 EINTR (Interrupted system call)
rt_sigsuspend([], 8) = ? ERESTARTNOHAND (To be restarted if no handler)
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=9762, si_uid=65534, si_status=0, si_utime=0, si_stime=0} ---
rt_sigreturn({mask=[HUP INT QUIT USR1 USR2 ALRM TERM CHLD WINCH IO]}) = -1 EINTR (Interrupted system call)
rt_sigsuspend([], 8) = ? ERESTARTNOHAND (To be restarted if no handler)
--- SIGIO {si_signo=SIGIO, si_code=SI_KERNEL} ---
rt_sigreturn({mask=[HUP INT QUIT USR1 USR2 ALRM TERM CHLD WINCH IO]}) = -1 EINTR (Interrupted system call)
rt_sigsuspend([], 8) = ? ERESTARTNOHAND (To be restarted if no handler)
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=9763, si_uid=65534, si_status=0, si_utime=0, si_stime=0} ---
rt_sigreturn({mask=[HUP INT QUIT USR1 USR2 ALRM TERM CHLD WINCH IO]}) = -1 EINTR (Interrupted system call)
rt_sigsuspend([], 8) = ? ERESTARTNOHAND (To be restarted if no handler)
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=9764, si_uid=65534, si_status=0, si_utime=0, si_stime=0} ---
--- SIGIO {si_signo=SIGIO, si_code=SI_KERNEL} ---
rt_sigreturn({mask=[CHLD]}) = 0
rt_sigreturn({mask=[HUP INT QUIT USR1 USR2 ALRM TERM CHLD WINCH IO]}) = -1 EINTR (Interrupted system call)
+++ exited with 0 +++
strace是一个最终信号的工具。扩展一下。如何关闭终端进程不退出呢?
方法:
- 拦截SIGHUP,然后自己处理
- 与bash进程不在同一个sesion中