To check the status of services you can use the power of systemctl
. For example you can query for all active systemd units with:
rpi ~$ systemctl --state=active --no-legend list-units systemd-*
systemd-ask-password-console.path loaded active waiting Dispatch Password Requests to Console Directory Watch
systemd-ask-password-wall.path loaded active waiting Forward Password Requests to Wall Directory Watch
[email protected] loaded active exited File System Check on /dev/mmcblk0p1
systemd-journal-flush.service loaded active exited Flush Journal to Persistent Storage
systemd-journald.service loaded active running Journal Service
systemd-logind.service loaded active running Login Service
systemd-modules-load.service loaded active exited Load Kernel Modules
systemd-random-seed.service loaded active exited Load/Save Random Seed
systemd-remount-fs.service loaded active exited Remount Root and Kernel File Systems
systemd-sysctl.service loaded active exited Apply Kernel Variables
systemd-sysusers.service loaded active exited Create System Users
systemd-timesyncd.service loaded active running Network Time Synchronization
systemd-tmpfiles-setup-dev.service loaded active exited Create Static Device Nodes in /dev
systemd-tmpfiles-setup.service loaded active exited Create Volatile Files and Directories
systemd-udev-trigger.service loaded active exited udev Coldplug all Devices
systemd-udevd.service loaded active running udev Kernel Device Manager
systemd-update-utmp.service loaded active exited Update UTMP about System Boot/Shutdown
systemd-user-sessions.service loaded active exited Permit User Sessions
systemd-fsckd.socket loaded active listening fsck to fsckd communication Socket
systemd-initctl.socket loaded active listening initctl Compatibility Named Pipe
systemd-journald-dev-log.socket loaded active running Journal Socket (/dev/log)
systemd-journald.socket loaded active running Journal Socket
systemd-rfkill.socket loaded active listening Load/Save RF Kill Switch Status /dev/rfkill Watch
systemd-udevd-control.socket loaded active running udev Control Socket
systemd-udevd-kernel.socket loaded active running udev Kernel Socket
systemd-tmpfiles-clean.timer loaded active waiting Daily Cleanup of Temporary Directories
You can query for --all
units instead of --state=active
. Then you will see what states are available that you can ask with --state=
, for example inactive
. Or just restrict it to one service with:
rpi ~$ systemctl --no-legend list-units systemd-timesyncd.service
systemd-timesyncd.service loaded active running Network Time Synchronization
Have a look at systemctl --help
or man systemctl
for many other query options.
If you decide to use systemctl from the command line, maybe in a system call from your application, then you also can use all its other options, e.g. to start/stop services with:
rpi ~$ sudo systemctl start/stop my.service
But because you want to make an application I strongly recommend to use the programming API of systemd. It uses the dbus as interface. You can look at The D-Bus API of systemd/PID 1 how to use it with your program.