You are not logged in.

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

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

[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
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 18,530
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  •   Github

Registered Linux User #482438

Offline

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

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 5,134

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

systemctl is-active

?


Sakura:-
Mobo: ASUS P8Z77-V PRO // Processor: Intel Core i7-3770K 3.4GHz // GFX: nVidia GeForce GTX 970 Ti // RAM: 32GB (4x 8GB) Corsair DDR3 (@ 2133MHz) // Storage: 1x 3TB Seagate SATAII 5x 1TB Samsung SATAII, 2x 120GB Corsair SSD

Offline

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

jasonwryan
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 18,530
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  •   Github

Registered Linux User #482438

Offline

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

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

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: 5,134

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: ASUS P8Z77-V PRO // Processor: Intel Core i7-3770K 3.4GHz // GFX: nVidia GeForce GTX 970 Ti // RAM: 32GB (4x 8GB) Corsair DDR3 (@ 2133MHz) // Storage: 1x 3TB Seagate SATAII 5x 1TB Samsung SATAII, 2x 120GB Corsair SSD

Offline

Board footer

Powered by FluxBB