Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tracking bash calls

I'd like a script/tool like understand C++(scitools) that give backtraces for a master bash script that call :

  • other bash scripts
  • Python
  • Java
  • Perl

to let me know what happend.

I don't need sophisticated graph, a simple text file is suficient.

What can let me do that?

like image 756
dlewin Avatar asked Apr 22 '26 10:04

dlewin


2 Answers

Using set -x or running the script using bash -x will print every line before it's executed (but after variable substitution, which can be useful, but can also be nasty, sometimes):

bash -x myScript.sh
like image 131
Joachim Sauer Avatar answered Apr 24 '26 00:04

Joachim Sauer


You may use strace to track your script's execution of other scripts or programs.

You will have to run your script like: strace -q -f -e execve yourscript.sh.

This will trace all calls made to other executables.

[root@devel ~]# ./x.sh 
x
y
z
[root@devel ~]# cat x.sh 
#!/bin/bash
echo x
./y.sh

[root@devel ~]# cat y.sh 
#!/bin/bash

echo y
./z.sh
[root@devel ~]# cat z.sh 
#!/bin/bash

echo z

[root@devel ~]# strace -q -f -e execve ./x.sh 
execve("./x.sh", ["./x.sh"], [/* 28 vars */]) = 0
x
[pid 19781] execve("./y.sh", ["./y.sh"], [/* 28 vars */]) = 0
y
[pid 19782] execve("./z.sh", ["./z.sh"], [/* 28 vars */]) = 0
z
[pid 19781] --- SIGCHLD (Child exited) @ 0 (0) ---
--- SIGCHLD (Child exited) @ 0 (0) —

It will trace even calls to perl or other executables.

[root@devel ~]# cat x.sh 
#!/bin/bash
echo x
./y.sh
ls >/dev/null 2>&1
[root@devel ~]# cat y.sh 
#!/bin/bash

echo y
perl -e 'print "z\n";'
[root@devel ~]# ./x.sh 
x
y
z
[root@devel ~]# strace -q -f -e execve ./x.sh 
execve("./x.sh", ["./x.sh"], [/* 28 vars */]) = 0
x
[pid 20300] execve("./y.sh", ["./y.sh"], [/* 28 vars */]) = 0
y
[pid 20301] execve("/usr/bin/perl", ["perl", "-e", "print \"z\\n\";"], [/* 28 vars */]) = 0
z
[pid 20300] --- SIGCHLD (Child exited) @ 0 (0) ---
--- SIGCHLD (Child exited) @ 0 (0) ---
[pid 20302] execve("/bin/ls", ["ls"], [/* 28 vars */]) = 0
--- SIGCHLD (Child exited) @ 0 (0) ---
[root@devel ~]# 
like image 29
Dan Vatca Avatar answered Apr 23 '26 23:04

Dan Vatca