Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i make the python program to check linux services

I want to make simple python script , which i can run on cron job. i just want to see if these services are currently running or stopped

Httpd
mysql

How should i check them with python.

Do i need to parse the output of netstat -tlnp

like image 441
user2424160 Avatar asked Nov 28 '22 02:11

user2424160


1 Answers

   import subprocess
    
    
    service = "apache2"
    
    p =  subprocess.Popen(["systemctl", "is-active",  service], stdout=subprocess.PIPE)
    (output, err) = p.communicate()
    output = output.decode('utf-8')
    
    print(output)

The Python program will check if the service is running. If it running, the output will be "active" otherwise the output will be "inactive".

Hope it helps!!

like image 196
Falcon Ryu Avatar answered Dec 05 '22 20:12

Falcon Ryu