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
1 2 3 4 5 6 |
#!/usr/bin/python from 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#!/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" |