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

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

问题描述

脚本行为异常.我需要知道谁调用了那个脚本,谁调用了调用脚本,等等,只需要修改行为不端的脚本.

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.

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

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.

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

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

然后您可以通过检查此输出来确定哪个进程调用了哪个其他进程.这可以手动完成,也可以使用 awk 之类的工具自动完成,因为输出是常规的 -​​ 您只需使用那些 PIDPPID 列即可找出您感兴趣的所有流程之间的关系.

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天全站免登陆