Use systemctl is-active to determine if a service is running
At work, we have some Linux VMs with services managed by systemctl.
If I want to check if a service is running, I can use systemctl status:
$ systemctl status ssh
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2025-10-23 00:35:20 UTC; 3 months 14 days ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 451 (sshd)
Tasks: 1 (limit: 37867)
Memory: 4.6M
CPU: 452ms
CGroup: /system.slice/ssh.service
└─451 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"If I want to automate the process of checking whether a service is running, I can use the is-active subcommand. For example:
$ systemctl is-active ssh
activeThis returns exit code 0 if the service is active, and non-zero otherwise. Adding --quiet will suppress the output.
This allows me to write shell scripts like:
if systemctl is-active --quiet ssh
then
echo "SSH is running"
else
echo "SSH is not running"
fiI’m using this in some scripts to ensure they don’t run while the service is running – I can check for is-active, and block further execution until the service is stopped.
References: