Hi, I would like to combine two shutdown scripts into one and was wondering if anyone could help me to achieve this. The first one is a python script that I found here. It monitors specific computers in the network and if they shutdown it will shut the server down as well:
The second one is a bash script by wilko16. It monitors the system for io activity and when this is idle for a certain amount of time it shuts the system down:
I would like to combine the two so that the system monitors the network for other computers. Only when they are shutdown it would start to monitor for io activity and when that is idle for the specified interval it shuts down the server.
Is this possible to do? Can I just replace the shutdown command in the python script with an execute command for the bash script? And can I then run this script as a cronjob every 5 minutes or would that start to create duplicates running once the conditions of the python script are met?
Thanks!
Code:
#!/usr/bin/env python from subprocess import Popen, PIPE import os, sys ip_list = [] ###### IP addresses ##### # IP addresses go underneath this line, one on each line in the format: ip_list.append('x.x.x.x') ###### End IP addresses shutdown = True for ip in ip_list: cmd = ['/sbin/ping', '-s 0', '-W 1', '-q', '-n' , '-c 1', ip] p1 = Popen(cmd, stdout=PIPE, stderr=PIPE) p1.wait() statusText = p1.communicate()[0].split('\n')[3].strip() if statusText.find(', 0.0% packet loss') > 0: shutdown = False if shutdown: cmd = ['/sbin/shutdown', '-p', 'now'] p1 = Popen(cmd)
The second one is a bash script by wilko16. It monitors the system for io activity and when this is idle for a certain amount of time it shuts the system down:
Code:
#!/bin/bash # Desired idle interval in seconds interval=240 while [ true ]; do if zpool iostat $interval 2 | tail +1 |tail +`zpool iostat |wc -l|sed "s/ *//;"`|grep -q "0 *0 *0 *$"; then shutdown -p now fi done
I would like to combine the two so that the system monitors the network for other computers. Only when they are shutdown it would start to monitor for io activity and when that is idle for the specified interval it shuts down the server.
Is this possible to do? Can I just replace the shutdown command in the python script with an execute command for the bash script? And can I then run this script as a cronjob every 5 minutes or would that start to create duplicates running once the conditions of the python script are met?
Thanks!