开发者

Run Linux commands from Daemon

开发者 https://www.devze.com 2023-01-07 23:21 出处:网络
I need to run a linux command such as \"df\" from my linux daemon to kn开发者_如何学Pythonow free space,used space, total size of the parition and other info.I have options like calling system,exec,po

I need to run a linux command such as "df" from my linux daemon to kn开发者_如何学Pythonow 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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消