Trace of executed programs called by a Bash script

后端 未结 8 1872
难免孤独
难免孤独 2020-12-13 14:44

A script is misbehaving. I need to know who calls that script, and who calls the calling script, and so on, only by modifying the misbehaving script.

This is similar

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-13 15:28

    A simple script I wrote some days ago...

    # FILE       : sctrace.sh
    # LICENSE    : GPL v2.0 (only)
    # PURPOSE    : print the recursive callers' list for a script
    #              (sort of a process backtrace)
    # USAGE      : [in a script] source sctrace.sh
    #
    # TESTED ON  :
    # - Linux, x86 32-bit, Bash 3.2.39(1)-release
    
    # REFERENCES:
    # [1]: http://tldp.org/LDP/abs/html/internalvariables.html#PROCCID
    # [2]: http://linux.die.net/man/5/proc
    # [3]: http://linux.about.com/library/cmd/blcmdl1_tac.htm
    
    #! /bin/bash
    
    TRACE=""
    CP=$$ # PID of the script itself [1]
    
    while true # safe because "all starts with init..."
    do
            CMDLINE=$(cat /proc/$CP/cmdline)
            PP=$(grep PPid /proc/$CP/status | awk '{ print $2; }') # [2]
            TRACE="$TRACE [$CP]:$CMDLINE\n"
            if [ "$CP" == "1" ]; then # we reach 'init' [PID 1] => backtrace end
                    break
            fi
            CP=$PP
    done
    echo "Backtrace of '$0'"
    echo -en "$TRACE" | tac | grep -n ":" # using tac to "print in reverse" [3]
    

    ... and a simple test.

    test

    I hope you like it.

提交回复
热议问题