linux - 通过运行脚本检测文件读/写

标签 linux shell io

我将得到一些 shell 脚本,我有点不知道它们会做什么。但我需要知道脚本执行哪些I/O 操作(例如它们读取/访问的文件、它们写入的文件、创建的文件夹...等)。

一个示例是使用 strace,如下所示:

输入脚本: g++ -Wall a.cpp -o hello

我将运行它: sudo strace -f -e trace=open g++ -Wall a.cpp -o hello 2> output.txt

*我想要什么:*output.txt 将包含跟踪以及已访问的文件(a.cpp 和 b.h,在 a.cpp 中使用,并输出 hello)

... 
[pid 13871] open("a.cpp", O_RDONLY|O_NOCTTY|O_LARGEFILE) = 3 
[pid 13871] open("b.h", O_RDONLY|O_NOCTTY|O_LARGEFILE) = 4 
... 
[pid 13874] open("hello", O_RDWR|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3
...

问题

1- strace 不处理 pipe 所以如果我得到像 mkdir d2 | 这样的命令cp *.c d2/ 它只会检测第一个命令mkdir d2

2- strace 提供了太多信息,这很好,但如果有更具体的信息那就太好了!

我搜索了其他工具和实用程序,其中大多数会列出正在运行的进程的信息,但我不希望这样,因为脚本将立即执行,什么也没有将出现在 Ubuntu 的进程列表中。

关于如何在 Ubuntu/Linux 中实现此目的有什么想法吗?

最佳答案

1)

strace doesnt handle pipe so if I get a command like mkdir d2 | cp *.c d2/ it will only detect the first command mkdir d2.

strace 有两个有用的选项

 -f  Trace child processes as they are created by currently traced processes as a result of the fork(2) system call.
-ff  If the -o filename option is in effect, each processes trace is written to filename.pid where pid is the numeric process id of each process.

你可以这样做:

strace -f -o res.txt bash -c 'mkdir d2 | cp *.c d2/'

或者你可以这样做:

strace -ff -o res.txt bash -c 'mkdir d2 | cp *.c d2/'

2)

strace provide too much info which is fine but if something is more concrete that would be great!

SystemTap “允许开发人员和管理员编写和重用简单的脚本来深入检查实时 Linux 系统的事件”。您可以编写自己的脚本,也可以使用可用的脚本。 This is a script来自检测打开文件的 SystemTap 网站:

stap your_script.stp -c "your command"

关于linux - 通过运行脚本检测文件读/写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20709617/

相关文章:

linux - 用户无法创建文件 linux

bash - 删除最终的 bash 脚本参数

bash - 如何在 shell 中的指定行号中添加或删除字符?

Android:在非根设备上运行脚本

c# - IO操作的并发问题

python - 在Python中创建一个与目录同名的文件

linux - 为什么只有在按回车后才能在串行连接中接收到字符?

linux - 无法以 root 身份运行 uwsgi, "bind(): Permission denied"

linux - 如何对 Linux 密码进行逆向工程?

io.netty.channel.AbstractChannel$AnnotatedConnectException : Connection Time out: No further Information: