You are not logged in.

#1 2012-12-06 20:35:17

maggie
Member
Registered: 2011-02-12
Posts: 255

[Systemd] - How to tell if a service is running in a bash script

I need a bash script to query systemd (systemctl or systemd or?) to see if a given service is running. Is there a way to do it talking to systemd directly, not by looking for a pid file or seeing if a process is running? Thank you.

Offline

#2 2012-12-06 20:39:24

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [Systemd] - How to tell if a service is running in a bash script

systemctl status $yourservice | grep running
echo $?

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2012-12-06 20:44:06

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 11,783
Website

Re: [Systemd] - How to tell if a service is running in a bash script

systemctl is-active

?


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Online

#4 2012-12-06 20:47:58

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [Systemd] - How to tell if a service is running in a bash script

WorMzy wrote:
systemctl is-active

?

Nice.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#5 2012-12-06 20:50:43

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: [Systemd] - How to tell if a service is running in a bash script

FWIW, active does not necessarily mean running:

$ systemctl status netcfg.service 
...
	  Active: active (exited) since Thu, 2012-12-06 10:09:13 EST; 5h 35min ago
...
$ systemctl is-active netcfg.service
active

But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#6 2012-12-06 22:37:22

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 11,783
Website

Re: [Systemd] - How to tell if a service is running in a bash script

Good point.

I'd say that, in the case of "RemainAfterExit=yes" services, it's not really black and white. The effects of the service are active, even if the service itself has exited, but it's up to the user to decide whether or not that's "running" in their opinion.


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Online

Board footer

Powered by FluxBB