Discussion:
Profile bash script
(too old to reply)
Peng Yu
2018-12-01 12:09:33 UTC
Permalink
Hi,

I’d like to check how frequent each external command is called by a bunch
of bash scripts (one may call another). On way to do so is to hook a call
back function at the finish of the execution of each external command. But
I don’t see such a feature in bash.

Another way is to wrap around each possible external command with a command
of the same name which is in a directory prepended to $PATH. Those wrappers
will record the times they are called, then call the original command with
the same name. But this is cumbersome.

Is a good way to record how many times each external command is called?
--
Regards,
Peng
Eduardo Bustamante
2018-12-01 16:55:05 UTC
Permalink
Post by Peng Yu
Hi,
I’d like to check how frequent each external command is called by a bunch
of bash scripts (one may call another).
You can use strace:

***@debian:~$ strace -fe execve -tt bash -c 'date; echo hi; hostname'
08:54:31.502301 execve("/bin/bash", ["bash", "-c", "date; echo hi;
hostname"], 0x7ffcafcf1c48 /* 33 vars */) = 0
strace: Process 2413 attached
[pid 2413] 08:54:31.515721 execve("/bin/date", ["date"],
0x556ac5071280 /* 33 vars */) = 0
Sat Dec 1 08:54:31 PST 2018
[pid 2413] 08:54:31.523498 +++ exited with 0 +++
08:54:31.523768 --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED,
si_pid=2413, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
hi
strace: Process 2414 attached
[pid 2414] 08:54:31.527918 execve("/bin/hostname", ["hostname"],
0x556ac5081f40 /* 33 vars */) = 0
debian
[pid 2414] 08:54:31.531035 +++ exited with 0 +++
08:54:31.531259 --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED,
si_pid=2414, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
08:54:31.531900 +++ exited with 0 +++

Loading...