从守护进程运行 Linux 命令

发布于 2024-09-10 16:53:58 字数 285 浏览 5 评论 0原文

我需要从我的 Linux 守护进程运行一个 Linux 命令,例如“df”,以了解可用空间、已用空间、分区的总大小和其他信息。我有诸如调用 system、exec、popen 等选项。

  1. 但由于每个命令都会生成一个新进程,因此是否不可能在调用它的同一进程中运行命令?

  2. 同时,我需要从 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..

  1. 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?

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以往的大感动 2024-09-17 16:53:58

没有标准 API,因为这是特定于操作系统的概念。

但是,

  1. 您可以使用(不可移植) getmntent/getmntent_r 辅助函数。
  2. 使用有关已挂载文件系统的信息,您可以通过 statfs

There is no standard API, as this is an OS-specific concept.

However,

  1. You can parse /proc/mounts (or /etc/mtab) with (non-portable) getmntent/getmntent_r helper functions.
  2. Using information about mounted filesystems, you can get its statistics with statfs.
站稳脚跟 2024-09-17 16:53:58

您可能会发现探索 i3status 程序源代码很有用:http://code.stapelberg.de/git/i3status/tree/src/print_disk_info.c

要回答您的其他问题:

但是,由于每个命令都会生成一个新进程,因此是否无法在调用它的同一进程中运行命令?

不;整个“命令”都是独立的程序,必须在自己的进程中运行。

根据您希望执行程序的频率,fork();exec() 还不错。没有任何硬性限制,超出该限制最好自己收集数据而不是执行帮助程序。每分钟一次,您可能可以很好地执行命令。每秒一次,您最好自己收集数据。我不确定分界线在哪里。

同时,我需要从 Linux 守护进程运行此命令,因为我的守护进程不应该拥有任何终端。它会影响我的守护进程行为吗?

如果命令调用 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.c

To answer your other questions:

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?

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.

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?

If the command calls setsid(2), then open(2) on a terminal without including O_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文