从守护进程运行 Linux 命令
我需要从我的 Linux 守护进程运行一个 Linux 命令,例如“df”,以了解可用空间、已用空间、分区的总大小和其他信息。我有诸如调用 system、exec、popen 等选项。
但由于每个命令都会生成一个新进程,因此是否不可能在调用它的同一进程中运行命令?
同时,我需要从 Linux 守护进程运行此命令,因为我的守护进程不应该拥有任何终端。它会影响我的守护进程行为吗?
或者他们有任何 C 或 C++ 标准 API 来获取已安装的分区信息
I need to run a linux command such as "df" from my linux daemon to know free space,used space, total size of the parition and other info. I have options like calling system,exec,popen etc..
But as this each command spawn a new process , is this not possible to run the commands in the same process from which it is invoked?
And at the same time as I need to run this command from a linux daemon, as my daemon should not hold any terminal. Will it effect my daemon behavior?
Or is their any C or C++ standard API for getting the mounted paritions information
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有标准 API,因为这是特定于操作系统的概念。
但是,
getmntent
/getmntent_r
辅助函数。statfs
。There is no standard API, as this is an OS-specific concept.
However,
/proc/mounts
(or/etc/mtab
) with (non-portable)getmntent
/getmntent_r
helper functions.statfs
.您可能会发现探索
i3status
程序源代码很有用:http://code.stapelberg.de/git/i3status/tree/src/print_disk_info.c要回答您的其他问题:
不;整个“命令”都是独立的程序,必须在自己的进程中运行。
根据您希望执行程序的频率,
fork();exec()
还不错。没有任何硬性限制,超出该限制最好自己收集数据而不是执行帮助程序。每分钟一次,您可能可以很好地执行命令。每秒一次,您最好自己收集数据。我不确定分界线在哪里。如果命令调用
setsid(2)
,然后在终端上open(2)
(不包括O_NOCTTY
),则该终端 可能 成为该进程的控制终端。但这不会影响您的程序,因为您的程序在成为守护进程时已经放弃了终端,并且由于子进程是会话领导者,因此它无法更改进程的控制终端。You may find it useful to explore the
i3status
program source code: http://code.stapelberg.de/git/i3status/tree/src/print_disk_info.cTo answer your other questions:
No; entire 'commands' are self-contained programs that must run in their own process.
Depending upon how often you wish to execute your programs,
fork();exec()
is not so bad. There's no hard limits beyond which it would be better to gather data yourself vs executing a helper program. Once a minute, you're probably fine executing the commands. Once a second, you're probably better off gathering the data yourself. I'm not sure where the dividing line is.If the command calls
setsid(2)
, thenopen(2)
on a terminal without includingO_NOCTTY
, that terminal might become the controlling terminal for that process. But that wouldn't influence your program, because your program already disowned the terminal when becoming a daemon, and as the child process is a session leader, it cannot change your process's controlling terminal.