如何捕获临时创建的文件?

Ser*_*sak 6 linux files

我有一个程序可以生成一些 shell 脚本,然后运行并删除它。是否有机会在不侵入生成该脚本的程序的情况下获取该脚本的内容?

Ser*_*sak 3

我发现了这个不平凡的解决方案:

#!/bin/sh

file_path=$1/script-file-name

while inotifywait -e create "$1"; test ! -f "$file_path"
do
  :
done

chmod g+rw "$file_path"
chown root "$file_path"
Run Code Online (Sandbox Code Playgroud)

root该脚本必须以 的权限运行:

sudo <catch-script> /dir/where/the/target/will/be/generated
Run Code Online (Sandbox Code Playgroud)

以前,必须按如下方式更改将生成脚本的目录:

chown root /dir/where/the/target/will/be/generated
chmod 1775 /dir/where/the/target/will/be/generated
Run Code Online (Sandbox Code Playgroud)

即我们将粘性位设置为放置脚本的目录。然后指定的(捕获)脚本等待创建目标脚本,为其设置写入权限(以便生成程序能够使用它)并将所有者更改为root(以禁用删除) 。