【问题标题】:How to monitor process status during process lifetime如何在进程生命周期内监控进程状态
【发布时间】: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.

【问题讨论】:

    标签: linux unix pid ps


    【解决方案1】:

    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.
    

    【讨论】:

    • 'strace' 是我正在寻找的。它完美地工作。谢谢你。
    【解决方案2】:

    如果您无法重新编译和检测 main.exec,循环中的ps 是一个可能适合您的简单选项:

    while true; do ps --ppid=<pid> --pid=<pid> -o pid,ppid,%cpu,... >> mytrace.txt; sleep 0.2; done
    

    然后相应地解析输出。

    top 也可以工作,并且可以在批处理模式下运行,但不确定您是否可以让它动态监控像ps 这样的子进程。不要这么想。

    【讨论】:

    • 目前我正在这样做(循环中的ps)
    猜你喜欢
    • 2013-04-06
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    相关资源
    最近更新 更多