脚本有问题。我需要知道谁调用了那个脚本,谁调用了调用脚本,等等,只需修改行为不当的脚本即可。
这类似于堆栈跟踪,但我不对单个 bash 脚本中函数调用的调用堆栈感兴趣。 相反,我需要由我的脚本启动的已执行程序/脚本链。
最佳答案
我前几天写的一个简单的脚本...
# 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]
...和一个简单的测试。
希望你喜欢。
关于linux - Bash 脚本调用的已执行程序的跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/685435/