所以我正在编写一个 bash 脚本,它将遍历/proc/[pid] 中的所有进程 ID,并读取用于运行它的可执行文件。
根据我的观察,/proc 文件系统包含/proc/[pid]/exe 符号链接(symbolic link)。在 bash 脚本中,我正在尝试找出如何读取“readlink/proc/[pid]/exe”的值以检查是否返回(已删除)或没有返回任何内容以查明原始可执行文件是否存在于磁盘上。
到目前为止,有没有办法做到这一点?
#!/bin/bash
pid = "0"
while [ $pid -lt 32769 ]
do
if [-d /proc/$pid]; then
if [-f /proc/$pid/exe]; then
echo $pid
readlink /proc/$pid/exe
fi
fi
pid = $[$pid+1]
done
这无法工作并且始终不返回任何内容。我正在尝试列出所有不再在磁盘上提供其可执行文件的进程。
最佳答案
这对你有用吗?
#!/bin/bash
for i in $(ls /proc | awk '/^[[:digit:]]+/{print $1}'); do
if [ -h /proc/$i/exe ]; then
echo -n "$i: "
if readlink /proc/$i/exe >/dev/null 2>&1 ; then
echo "executable exists"
else
echo "executable not found"
fi
fi
done
关于Linux:在 Bash 脚本中读取 readlink/proc/pid/exe 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30575310/