如何检测Linux上程序的启动? [英] How to detect the launching of programs on Linux?

查看:67
本文介绍了如何检测Linux上程序的启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的守护程序. 当我运行任何程序时,该守护程序都应响应. 这该怎么做? 在一个大型守护程序循环中:

I wrote a simple daemon. This daemon should respond when I run any program. How to do this? In a big daemon loop:

while(1)
{
   /* function which catches new programm running */
}

当我运行新程序(创建新进程)时,在Linux中要调用什么函数?

What functions to call in linux, when i'm running a new program (create new process)?

推荐答案

我不知道是否存在更好的方法,但是您可以定期扫描/proc文件系统.

I don't know if there exists a better way, but you could periodically scan the /proc filesystem.

例如,/proc/<pid>/exe是到该进程的可执行文件的符号链接.

For example, /proc/<pid>/exe is a symlink to the process's executable.

在我的系统(Ubuntu/RedHat)上,/proc/loadavg包含正在运行的进程数(正斜杠后的数字)以及最近启动的进程的pid.如果守护程序轮询文件,则对两个数字之一进行的任何更改都会告诉它何时需要重新扫描/proc寻找新进程.

On my systems (Ubuntu/RedHat), /proc/loadavg contains the number of running processes (the number after the forward slash) as well as the pid of the most recently started process. If your daemon polls the file, any change to either of the two numbers will tell it when it needs to re-scan /proc looking for new processes.

这绝不是防弹的,而是我能想到的最合适的机制.

This is by no means bullet-proof, but is the most suitable mechanism I can think of.

这篇关于如何检测Linux上程序的启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆