How to monitor process status during process lifetime

813 views Asked by At

I need to track the process status ps axf during executable lifetime. Let's say I have executable main.exec and want to store into a file all subprocess which are called during main.exec execution.

 $ main.exec &
 $ echo $! # and redirect every ps change for PID $! in a file.
2

There are 2 answers

1
helloV On BEST ANSWER

strace - trace system calls and signals

$ 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.
1
spinkus On

If you can't recompile and instrument main.exec, ps in a loop is a simple option that may work for you:

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

Then parse the output accordingly.

top may also work, and can run in batch mode but not sure if you can get it to dynamically monitor child processes like ps. Don't think so.