You are not logged in.

#1 2016-05-09 15:40:21

losko
Member
Registered: 2014-11-19
Posts: 42

[solved] Get systemd service status in a script

Hi there,
I'm writing a script that automates some tasks for maintenance.
Sometimes some systemd services may need to be reloaded.
Here I edited the service on the fly for testing:

Warning: bridge@enp6s4.service changed on disk. Run 'systemctl daemon-reload' to reload units.

How can I catch this "state" in a script?
For some reason 'systemctl status bridge@enp6s4.service' returns always 0.

[losko] (~) $ sudo systemctl status bridge@enp6s4.service
● bridge@enp6s4.service - Network bridged connectivity (enp6s4)
   Loaded: loaded (/etc/systemd/system/bridge@.service; enabled; vendor preset: disabled)
   Active: active (exited) since lun 2016-05-09 11:48:07 CEST; 5h 10min ago
 Main PID: 286 (code=exited, status=0/SUCCESS)

mag 09 11:48:07 archbox systemd[1]: Starting Network bridged connectivity (enp6s4)...
mag 09 11:48:07 archbox systemd[1]: Started Network bridged connectivity (enp6s4).
Warning: bridge@enp6s4.service changed on disk. Run 'systemctl daemon-reload' to reload units.

[losko] (~) $ echo $?
0

For some other reason that "Warning" line in the output is not grep-able...
Thanks.

Last edited by losko (2016-05-09 16:07:15)


"Greetings from the Banana Republic"

Offline

#2 2016-05-09 15:42:10

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: [solved] Get systemd service status in a script

What do you mean it is not grep-able?  Why not?  How are you trying to grep it?  Are you grepping the stderr or the stdout?  Or both?


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2016-05-09 15:50:18

losko
Member
Registered: 2014-11-19
Posts: 42

Re: [solved] Get systemd service status in a script

Yes I tried this way:

[losko] (~) $ VAR=$(sudo systemctl status bridge@enp6s4.service | grep -o "daemon-reload")
Warning: bridge@enp6s4.service changed on disk. Run 'systemctl daemon-reload' to reload units.
[losko] (~) $ echo $VAR

"Greetings from the Banana Republic"

Offline

#4 2016-05-09 15:53:52

Scimmia
Fellow
Registered: 2012-09-01
Posts: 11,544

Re: [solved] Get systemd service status in a script

You see how the warning is still printed? That's a clue that it's not in stdout. You need to redirect stderr.

Online

#5 2016-05-09 15:57:04

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: [solved] Get systemd service status in a script

if systemctl status bridge@enp6s4.service 2>&1 | grep -q "daemon-reload"; then
   # do your stuff here
fi

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2016-05-09 16:03:03

losko
Member
Registered: 2014-11-19
Posts: 42

Re: [solved] Get systemd service status in a script

[losko] (~) $ VAR=$(sudo systemctl status bridge@enp6s4.service 2>&1 | grep -o "daemon-reload")
[losko] (~) $ echo $VAR
daemon-reload

My bad, the stderr was the solution...
Marking as solved.
Thanks


"Greetings from the Banana Republic"

Offline

#7 2016-05-09 16:23:25

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: [solved] Get systemd service status in a script

Also note that you don't need sudo there (and thus shouldn't use it).  Aslo you should consider grep's -q flag if you are just testing whether a match is found.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2016-05-09 19:35:17

losko
Member
Registered: 2014-11-19
Posts: 42

Re: [solved] Get systemd service status in a script

Ok for the sudo, I was just testing locally, hoping to find a solution to put on my server.
The script is triggered by a systemd-timer, so no need for sudo at all...
I never considered using 'grep -q' for testing... nice.
The working solution looks like:

for file in /etc/systemd/system/multi-user.target.wants/*
do
    if systemctl status ${file##*/} 2>&1 | grep -q "daemon-reload"
    then
        systemctl daemon-reload
        break
    fi
done

Thanks again.


"Greetings from the Banana Republic"

Offline

#9 2016-05-09 20:00:24

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: [solved] Get systemd service status in a script

There is no need for the loop:

systemctl status -a 2>&1 | grep -q '^Warn.*daemon-reload' && systemctl daemon-reload

I'm also wondering if there isn't a yet-more-streamlined approach to this.  I'd not be surprised if there were a better set of options to systemctl to get just what you want without having to grep the stderr.  However it didn't jump out at me from a quick scan of the man page.

You could direct the stdout to /dev/null so only the stderr is passed to grep.  This would substantially reduce the number of lines grep needs to compare, and you could also simplify the pattern again.  But such OCD-level optimizations really will not have any noteworthy effect:

systemctl status -a 2>&1 >/dev/null | grep -q 'daemon-reload' && systemctl daemon-reload

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

Board footer

Powered by FluxBB