Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python code to check if service is running or not.?

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

like image 579
S Andrew Avatar asked Oct 05 '17 06:10

S Andrew


1 Answers

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.
like image 185
Dylan Westra Avatar answered Oct 13 '22 02:10

Dylan Westra