Joyer
Joyer

Reputation: 431

How can I list all processes got executed during a shell-script(or sth. like that) running

For example. When the following script gets executed:

#!/bin/sh
g++ -o foo foo.cpp 
sudo apt-get install ninja

Is there any clean way to list the following process records?

sh
g++
cpp
cplusplus
gas
ld
sudo
perl

Upvotes: 0

Views: 241

Answers (1)

user184968
user184968

Reputation:

You can use strace in order to find all programs that were executed while your script was running. Something like this:

strace -o executed.txt -e execve -f sh your-sctrip.sh 

This is an example. First my bash script:

$ cat build.sh
#!/bin/sh
g++ main.cpp
ls

Then strace of it:

$ strace 2>&1 -e execve -f bash build.sh \
   | sed -n -r "s/.* execve\(\"([^,\"]+)\".* = 0$/\1/p"
/usr/local/CC/gcc-4.3.3/bin/g++
/usr/local/CC/gcc-4.3.3/libexec/gcc/x86_64-unknown-linux-gnu/4.3.3/cc1plus
/usr/bin/as
/usr/local/CC/gcc-4.3.3/libexec/gcc/x86_64-unknown-linux-gnu/4.3.3/collect2
/usr/bin/ld
/bin/ls

Or if you need only filenames add xargs -n 1 basename:

$ strace 2>&1 -e execve -f bash build.sh \
    | sed -n -r "s/.* execve\(\"([^,\"]+)\".* = 0$/\1/p" \
    | xargs -n 1 basename
g++
cc1plus
as
collect2
ld
ls

Upvotes: 1

Related Questions