繁体   English   中英

Linux Shell-如果文件已删除,如何查找所有正在运行的进程

[英]Linux shell - How to find for all running process if the file has been removed

我想确定是否有一个正在运行的进程(相关进程的文件)不再存在。

到目前为止,我有:

# [ ! -f /my/file ]; than echo "NOT exist!"; fi

了解文件是否存在

现在,我需要从进程列表中提取生成该进程的文件,删除参数,并验证文件是否存在...但是我正在努力使其正常运行。 另外,我必须注意以下过程:/ bin / sh /my/script.sh / usr / bin / python / my / python_file

(对于第二种情况,我可以使用“ lsof | grep delete”部分地识别出script.sh或python_file是否已被删除)

您知道是否有聪明的方法吗? 谢谢dk

lsof将已删除的文件列为/ path / to / file(已删除)

因此,作为root用户,这应该为您提供所有已删除文件的列表: grep'(已删除)'

之后,可以通过将文件名与可执行文件名进行匹配来缩短列表(如果匹配,则可能是正在运行的已删除可执行文件)。

我还将匹配所有已知运行脚本(ruby,perl等)的可执行文件。 更好的是,我宁愿过滤掉不执行的可执行文件,并手动验证每个剩余的误报。

证明

编译这个小程序:

int
main(int argc, char *argv[]) {
    for ( ; ; ) {
    }

    return 0;
}

$ gcc useless.c -o useless

运行它

$ ./useless

然后将其删除(在另一个终端中):

$ rm useless

以root身份运行lsof显示:

useless     738             dioo  txt       REG                8,4       7955      40685 /home/dioo/useless (deleted)

由于您使用的是Linux,因此我会通过/ proc进行拖曳

shopt -s extglob
for dir in /proc/+([0-9]); do
    # examine $dir/comm and decide if this is a script interpreter
    # like bash/python/perl ...

    # if yes, get the script name with
    my_script=$( cut -d '' -f 2 $dir/cmdline )

    # decide if the program or script file is missing
done

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM