Huskie69
Huskie69

Reputation: 865

Capture historical process history UNIX?

I'm wondering if there a way of capturing a list of the processes executed on a non-interactive shell?

Basically I have a script which calls some variables from other sources and I want to see what the values of said variables are. However, the script executes and finishes very quickly so I can't capture the values using ps.

Is there a way to log processes and what arguments were used?

TIA Huskie

EDIT:

I'm using Solaris in this instance. I even thought about about having a quick looping script to capture the values being passed - but this doesn't seem very accurate and I'm sure executions aren't always being captured. I tried this:

   #!/bin/ksh

   while [ true ]
   do

   ps -ef | grep  $SCRIPT_NAME |egrep -v 'shl|lis|grep' >> grep_out.txt

   done

I'd use sleep but I can't specify any precision as all my sleep executables want an integer value rather than any fractional value.

Upvotes: 0

Views: 4645

Answers (3)

jlliagre
jlliagre

Reputation: 30813

On Solaris:

truss -s!all -daDf -t exec yourCommand 2>&1 | grep -v ENOENT

On AIX and possibly other System V based OSes:

truss -s!all -daDf -t execve yourCommand 2>&1 | grep -v ENOENT

On Linux and other OSes supporting strace, you can use this command:

strace -ff -etrace=execve yourCommand 2>&1 >/dev/tty | grep -v ENOENT

In case the command you want to trace is already running, you can replace yourCommand by -p pid with pid being the process to be traced process id.

EDIT:

Here is a way to trace your running script(s) under Solaris:

for pid in $(pgrep -f $SCRIPT_NAME); do
    truss -s!all -daDf -t exec -p $pid 2>&1 | grep -v ENOENT > log.$pid.out &
done

Note that with Solaris, you might also use dtrace to get the same (and more).

Upvotes: 3

isedev
isedev

Reputation: 19601

Most shells can be invoked in debug mode, where each statement being executed is printed to stdout (or stderr) after variable substitution and expansion.

For Bourne like shells (sh, bash), debug is enabled with the -x option (as in bash -x myscript) or using the set -x statement within the script itself.

However, debugging only works for the 'current' script. If the script calls other scripts, these other scripts will not execute in debug mode. Furthermore, the code inside functions may not be executed in debug mode either - depends on the specific shell - although you can use set -x within a function to enable debug explicitly.

Upvotes: 3

Etan Reisner
Etan Reisner

Reputation: 80931

A very much more verbose (at least by default) option is to use something like strace for this.

strace -f -o trace.out script.sh

will give you huge amounts of information about what the script is doing. For your specific usage you will likely want to limit the output a bit with the -e trace=.... option to control which system calls are traced.

Use truss instead of strace on Solaris. Use dtruss on OS X (I believe). With appropriate command line argument changes as well.

Upvotes: 2

Related Questions