I have a Raspberry Pi which is running well, and am looking at creating a status app for it. I have some services that are run on startup, and I want my app to check that they are running, and if not then start them (or offer the option for me to attempt to start them).

But from what I can tell there are multiple ways to start/stop/check status of services, eg systemctl, service, possibly others that I don't recall right now.

How can I determine how any specified service was started, or to put it another way, what commands I should use if I wanted to start/stop/check my services?

Running Raspbian Stretch on a Pi 3b+


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
systemd-fsck@dev-mmcblk0p1.service 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.


You can use this bash code and call this script with sudo:


if [ -z "$(pidof ${SERVICE_PID_NAME})" ];then
  echo "[!] service ${SERVICE_PID_NAME} is not running...";
  echo "I'm starting the service ${SERVICE_PID_NAME}";
  systemctl start ${SERVICE_PID_NAME}
  echo "[+] service ${SERVICE_PID_NAME} is already running...";

Adjust the SERVICE_PID_NAME variable with your own process name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.