I am using python2.7 to check if a service is running or not. I have made my own service and placed it inside /etc/init.d
. I have a raspberry on which I am using it.
Now to normally check the status of service, we can do:
service my_service status
But how can I get the status of service from the python code.
Thanks
I might be a few years late to answer this.. but here is an easy solution I've found
import os # I think it's better to use subprocess for this. but quick code for example
status = os.system('systemctl is-active --quiet service-name')
print(status) # will return 0 for active else inactive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With