4

How to programmatically check if particular daemon is running by specifying its name on Linux using C, C++ ?

Raju
  • 51
  • 4
  • 3
    You should search systemd's documentation. But just for curiosity, is this a [XY Problem](http://xyproblem.info)? From my experience when someone is asking this kind of question, they actually want to do something else. – Iharob Al Asimi Dec 14 '16 at 08:47
  • 1
    Also, note that doing this can be a security risk because presumably the program would have to have root privileges in order to query the required data. – Iharob Al Asimi Dec 14 '16 at 08:53
  • Maybe use something similar to this, and e.g. parse output of 'ps aux | grep ': http://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-output-of-command-within-c-using-posix – Erik Alapää Dec 14 '16 at 09:38
  • Largely dependent on which init system you are running (SysV, systemd, runit, etc) - you may get more informed answers over on the [unix.se] site. – Toby Speight Dec 14 '16 at 10:29

3 Answers3

1

A simple command to run would be systemctl status ${service} | grep "Active:" | cut -d' ' -f6. Capture the stdout of the above command and it should equal (running).

On that note, *ctl are intended for command line use only and not for use by other programs. The accepted way is to interact with systemd using the dbus API.

https://www.freedesktop.org/wiki/Software/systemd/dbus/

ckousik
  • 11
  • 3
0

As I cannot add comment above. you can use systemctl to check service status.

systemctl status servicename

This is terminal command. using system("systemctl status servicename") u can manage call from c++.

Hope this is what is were looking at or similar.

  • 1
    thanks!....Looks like in my CentOS this command is not available. its shows me following error: root@t1024rdb:~# systemctl -sh: systemctl: command not found – Raju Dec 16 '16 at 10:55
-2

systemctl is-active ${service_name} can be used to check if a service is active or not

R.Amutha
  • 1
  • 2