tags:

views:

18

answers:

2

Hello,

I want to know what executables a script launches and in what order (and trace those executables recursively).

For example, let's say I have a bash script here (called abc.sh):

#!/bin/bash
ls
gcc

I'd like to run this script with a "trace/log command" and get something like this:

abc.sh -- ls
      |-- gcc -- cpp
              -- cc1
              ... etc.

This show that abc.sh called ls, and gcc. gcc, in turn, called cpp and cc1.

Is this possible? How can I do it? A tree view like the above would be nice, but a simpler view would work as well.

Note that I do not want the current snapshot of the process tree. Rather, I want a trace or a log of the spawning processes. Timing annotation would also be useful.

Thanks!

A: 

You can use pstree command. It was conceived to show exactly what you are looking for: process tree (hence the name, pstree). It won't give you process tracing, but you could call it multiple times to get something similar to what you're looking fOr.

Pablo Santa Cruz
What if the process spawns and exits very quickly?
madiyaan damha
+1  A: 

You may try to analyse the output of strace command. Particularly, you'll be interested in something like

strace -f -tt -e trace=execve ./abc.sh
Roman Cheplyaka
Paul Rubel
This, this will work I think. I was hoping of some higher level script/tool that will do the following:1. If there are commands other than execve that can spawn children, catch those as well.2. Present it in a nice tree form.3. Not print the unsuccessful execve commands (normally it goes through your entire PATH to see if the exe is present there).
madiyaan damha
@Paul: I'd rather use -o option to redirect strace output to a file. Cause nothing prevents the program to print something containing "exec" to stderr, right?
Roman Cheplyaka
@madiyaan: you may also want to track fork/vfork/clone calls which lead to creating of new processes, but in fact execve is a signle syscall which leads to execution of a new program.
Roman Cheplyaka