I'm writing a script to automatically install a bind server on a CentOs 7 distribution.

I'm stuck with systemctl status, because it does not produce an error code (it's right, since a status is not an error) I can use.

What I want is to check whether the service is started (active). What is the best and efficient way to do this?

share|improve this question
    
Actually systemctl status does return a status - as I found when doing systemctl status openvpn@<>. Where the values are 0 for running, and 3 for stopped. However, this command is interactive :(. Hence the @lars suggested systemctl is-active is the way to go, and better att the -q as suggested by @palswim – SACHIN GARG Jul 14 '16 at 18:51

The best way to check if a service is active is with the systemctl is-active command:

# systemctl start sshd
# systemctl is-active sshd >/dev/null 2>&1 && echo YES || echo NO
YES
# systemctl stop sshd
# systemctl is-active sshd >/dev/null 2>&1 && echo YES || echo NO
NO
share|improve this answer
    
Thanks, that's exactly what i was looking for! it seems i missed it in the manual. ^^' – math May 6 '15 at 7:28
    
good stuff, thank you! – Assaf Moldavsky Mar 19 '16 at 16:49
8  
You can also use the -q switch so that you don't have to redirect output: systemctl -q is-active sshd – palswim Apr 5 '16 at 18:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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