【发布时间】:2014-11-23 00:38:53
【问题描述】:
我需要在可执行生命周期内跟踪进程状态ps axf。
假设我有可执行的 main.exec 并希望将在 main.exec 执行期间调用的所有子进程存储到一个文件中。
$ main.exec &
$ echo $! # and redirect every ps change for PID $! in a file.
【问题讨论】:
我需要在可执行生命周期内跟踪进程状态ps axf。
假设我有可执行的 main.exec 并希望将在 main.exec 执行期间调用的所有子进程存储到一个文件中。
$ main.exec &
$ echo $! # and redirect every ps change for PID $! in a file.
【问题讨论】:
strace - 跟踪系统调用和信号
$ main.exec &
$ strace -f -p $! -o child.txt
-f Trace child processes as they are created by currently traced processes as a result of the fork(2), vfork(2) and clone(2) system calls. Note that -p PID -f will attach all threads of process PID if it is multi-threaded, not only thread with thread_id = PID.
【讨论】:
如果您无法重新编译和检测 main.exec,循环中的ps 是一个可能适合您的简单选项:
while true; do ps --ppid=<pid> --pid=<pid> -o pid,ppid,%cpu,... >> mytrace.txt; sleep 0.2; done
然后相应地解析输出。
top 也可以工作,并且可以在批处理模式下运行,但不确定您是否可以让它动态监控像ps 这样的子进程。不要这么想。
【讨论】: