【问题标题】:Check if a already running process has the stdin redirected on Linux?检查已经运行的进程是否在 Linux 上重定向了标准输入?
【发布时间】:2019-04-07 15:58:34
【问题描述】:

给定一个进程的PID,有没有办法找出这个进程是否重定向了标准输入或标准输出?

我有一个从标准输入读取的应用程序。为方便起见,我通常使用从文件重定向的标准输入来启动此应用程序,如下所示:

app < input1.txt

问题是有时我启动应用程序并忘记我使用的输入文件是什么。有没有办法找出哪个文件被用来重定向标准输入?

使用ps -aux | grep PID 允许我查看使用的命令行。但没有给我任何关于stdinstdout 的信息。

我也尝试查看top/proc/PID/*,但没有找到任何东西。

我正在使用 CentOS 7,如果有帮助的话。

【问题讨论】:

标签: linux terminal process


【解决方案1】:

您应该能够查看/proc/<PID>/fd 以获取此信息。例如,如果我为来自文件的命令重定向标准输入:

sleep inf < somefile.txt

然后我在对应的/proc目录下填写find:

$ ls -l /proc/12345/fd
lr-x------. 1 lars lars 64 Nov  4 21:43 0 -> /home/lars/somefile.txt
lrwx------. 1 lars lars 64 Nov  4 21:43 1 -> /dev/pts/5
lrwx------. 1 lars lars 64 Nov  4 21:43 2 -> /dev/pts/5

将标准输出重定向到文件时,同样的事情。如果我跑:

sleep inf > somefile.txt

然后我看到了:

$ ls -l /proc/23456/fd
lrwx------. 1 lars lars 64 Nov  4 21:45 0 -> /dev/pts/5
l-wx------. 1 lars lars 64 Nov  4 21:45 1 -> /home/lars/somefile.txt
lrwx------. 1 lars lars 64 Nov  4 21:45 2 -> /dev/pts/5

【讨论】:

    猜你喜欢
    • 2011-01-06
    • 1970-01-01
    • 2014-01-30
    • 2011-02-12
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多