跟踪由Bash脚本调用的已执行程序 [英] Trace of executed programs called by a Bash script

查看:66
本文介绍了跟踪由Bash脚本调用的已执行程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

脚本行为异常.我只需要通过修改行为异常的脚本就可以知道谁调用了该脚本,谁调用了调用脚本,等等.

这类似于堆栈跟踪,但我对单个bash脚本中的函数调用的调用堆栈不感兴趣. 相反,我需要由我的脚本启动的已执行程序/脚本链.

解决方案

由于您说可以编辑脚本本身,因此只需输入:

ps -ef >/tmp/bash_stack_trace.$$

出现问题的地方.

这将在您的tmp目录中创建许多文件,这些文件会在发生时显示整个进程列表.

然后可以通过检查此输出来确定哪个进程调用了另一个进程.由于输出是常规的,因此可以手动执行,也可以使用awk之类的方法自动执行,因为输出是常规的-您只需使用这些PIDPPID列来计算出您感兴趣的所有流程之间的关系即可. /p>

您需要密切注意文件,因为每个进程都会得到一个文件,因此可能需要对其进行管理.由于只有在调试期间才能执行此操作,因此大多数情况下该行将被注释掉(以#开头),因此将不会创建文件.

要清理它们,只需执行以下操作:

rm /tmp/bash_stack_trace.*

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 to a stack-trace, but I am not interested in a call stack of function calls within a single bash script. Instead, I need the chain of executed programs/scripts that is initiated by my script.

解决方案

Since you say you can edit the script itself, simply put a:

ps -ef >/tmp/bash_stack_trace.$$

in it, where the problem is occurring.

This will create a number of files in your tmp directory that show the entire process list at the time it happened.

You can then work out which process called which other process by examining this output. This can either be done manually, or automated with something like awk, since the output is regular - you just use those PID and PPID columns to work out the relationships between all the processes you're interested in.

You'll need to keep an eye on the files, since you'll get one per process so they may have to be managed. Since this is something that should only be done during debugging, most of the time that line will be commented out (preceded by #), so the files won't be created.

To clean them up, you can simply do:

rm /tmp/bash_stack_trace.*

这篇关于跟踪由Bash脚本调用的已执行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆