检测在Linux平台上启动程序 [英] Detect launching of programs on Linux platform

查看:113
本文介绍了检测在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的/红帽),的/ 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天全站免登陆