python script to check server status 11. May / Python / No Comments Today lets write a simple script to chek the Apache server status using python. create a simple file called server_check on your desktop, copy and paste the below code #!/usr/bin/python from subprocess import call command = raw_input('Please enter service name : ') call(["/etc/init.d/"+command, "status"]) 123456 #!/usr/bin/pythonfrom subprocess import call command = raw_input('Please enter service name : ') call(["/etc/init.d/"+command, "status"]) Now lets execute and check. open your terminal and navigate to the desktop and type the following command. python check_service then it will ask for a service name “Please enter service name :” enter the service name eg: apache2 or mysql you will see a output like this. Today. i Upgraded the script, so that it can be more usable on server. This will support on Python 2.7+ only. checkout the sample script below. #!/usr/bin/python import subprocess checkIP = subprocess.check_output('ip addr | grep "10.200.181.56"', shell=True) if ("inet 10.200.181.56/24" in checkIP): services = ['san-mount', 'mysqld', 'httpd', 'vsftpd', 'nagios', 'crond', 'postfix', 'mysql', 'apache2'] for service in services: status = subprocess.check_output("/etc/init.d/"+service+" status", shell=True) if ("is stopped" in status): print service + " - Stopped" print service + " - Trying to start" service_start = subprocess.check_output("/etc/init.d/"+service+" start", shell=True) else: print service + " - Running " else: print "Ip Address not mounted cannot execute further" 12345678910111213141516 #!/usr/bin/pythonimport subprocess checkIP = subprocess.check_output('ip addr | grep "10.200.181.56"', shell=True)if ("inet 10.200.181.56/24" in checkIP): services = ['san-mount', 'mysqld', 'httpd', 'vsftpd', 'nagios', 'crond', 'postfix', 'mysql', 'apache2'] for service in services: status = subprocess.check_output("/etc/init.d/"+service+" status", shell=True) if ("is stopped" in status): print service + " - Stopped" print service + " - Trying to start" service_start = subprocess.check_output("/etc/init.d/"+service+" start", shell=True) else: print service + " - Running "else: print "Ip Address not mounted cannot execute further" sathyabaman I am a mobile application consultant with 7 years of experience in IT industry .
python script to check server status
Today lets write a simple script to chek the Apache server status using python.
create a simple file called server_check on your desktop, copy and paste the below code
Now lets execute and check.
open your terminal and navigate to the desktop and type the following command.
python check_service
then it will ask for a service name “Please enter service name :”
enter the service name eg: apache2 or mysql
you will see a output like this.
Today. i Upgraded the script, so that it can be more usable on server. This will support on Python 2.7+ only. checkout the sample script below.
sathyabaman
I am a mobile application consultant with 7 years of experience in IT industry .
You might also like
Loading images from server in to Android Grid view
Next ArticleUnable to compile android project - dependency issue