【问题标题】:cat: pid.txt: No such file or directorycat: pid.txt: 没有这样的文件或目录
【发布时间】:2021-02-07 23:40:31
【问题描述】:

我对猫有意见。我想编写与 ps -e 相同的脚本。在 pid.txt 我有正在运行的进程的 PID。

ls /proc/ | grep -o "[0-9]" | sort -h > pid.txt

然后我想使用 $line 作为 evry PID 的 cmdline 路径的一部分。

cat pid.txt | while read line; do cat /proc/$line/cmdline; done

我也试试 for 循环

for id in 'ls /proc/ | grep -o "[0-9]\+" | sort -h'; do 
cat /proc/$id/cmdline;
done

不知道我做错了什么。提前致谢。

【问题讨论】:

  • ls /proc/ 的输出会为您呈现什么...?
  • 如果必须使用ls,则使用ls -1 /proc/,不要使用grep -o,而只需使用grep '[0-9]'

标签: linux cat ps


【解决方案1】:

我认为您所追求的是这个 - 您的所有方法都存在一些缺陷(或者您真的只想查看具有一位数 PID 的进程?):

for pid in $(ls /proc/ | grep -E '^[0-9]+$'|sort -h); do cat /proc/${pid}/cmdline; tr '\x00' '\n'; done

【讨论】:

    【解决方案2】:

    与运行 ls... 命令时相比,运行 cat pid.txt... 命令时,您似乎位于不同的当前目录。在同一个终端窗口上运行两个命令,或使用绝对路径,如/path/to/pid.txt

    除了您的错误之外,您可能还想从 grep 命令中删除 -o,因为它会为您提供匹配 pid 的 1 位数字。例如,当 pid 为 423 时,您会得到 2。@Roadowl 也已经指出了这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-02
      • 2021-06-24
      • 2015-02-20
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      相关资源
      最近更新 更多