Linux:在 Bash 脚本中读取 readlink/proc/pid/exe 的输出

标签 linux bash linux-kernel procfs

所以我正在编写一个 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/

相关文章:

arrays - 一个文本文件中的两个数组

linux - 通过 CTRL-C 接收 SIGINT 时发送者 pid 为零

linux - 错误 : modinfo: could not find module

c++ - 启动停止守护进程不发送 SIGTERM

linux - 查找 -exec 和 Bash 脚本

linux - Linux 中多线程应用程序中的 Coredump

linux - VmSize = 物理内存 + swap?

计算出的 tcp 校验和与原始 tcp 校验和不匹配

php - 如何从 php/html 执行 shell 脚本?

bash - 循环让 youtube-dl 下载其中一种格式(取决于错误)