Combining two shutdown scripts - monitor clients + io activity

Status
Not open for further replies.

Freddy500

Cadet
Joined
Jan 29, 2014
Messages
6
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:

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!
 

Neil Whitworth

Dabbler
Joined
Nov 14, 2013
Messages
30
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:

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!


If I am reading this correctly, the first python script will shut down the system UNLESS one (or more) of the listed IP addresses are available.

The second script will loop forever, checking for idle io.

If you change the second script to call your python script instead of directly shutting down it should do what you want. Assuming your first script is called shutdown.py and in the same directory this should be what you need.

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.py
fi
done
 

Freddy500

Cadet
Joined
Jan 29, 2014
Messages
6
Yes, that worked perfectly when I added ./shutdown.py to the second script. Thanks for the tip! I also replaced the python script with a slightly more sophisticated one made by Jan-Philip Gehrcke that I found here. It will ping multiple times instead of just once.

In the same comments I also found a third script that seems interesting since it checks for SSH, Console activity, filesystem activity and mapped shares before shutting down. It was posted by a user named Andreas and he writes the follwing on it

I recently wrote an autoshutdown daemon in python, which is a bit more sophisticated. It checks for SSH / Console activity, filesystem activity and mapped shares. Advantages:

- Does not shut down if a local sync via ssh is performed
- Shuts down if no share is mapped or used and no other activity is detected

Link: http://fynder.de/autoshutdown.txt

Install:
- Open SSH connection, type
mount -uw / # mount / rw
wget -c http://fynder.de/autoshutdown.txt -O /usr/local/sbin/autoshutdown.py
chmod +x /usr/local/sbin/autoshutdown.py

To run at startup an /conf/base/etc/rc.local must exist:

#!/bin/sh
/usr/local/sbin/autoshutdown.py start

Don't forget:
chmod +x /conf/base/etc/rc.local

Daemon should work after reboot, test it with:
cat /tmp/autoshutdown.pid

I cannot seem to get it to work though. I placed it in a .py file, made it executable and tried to test it with:

# ./shutdown.py start

It doesn't give me any errors but there is nothing listed when i do a ps command. What am I doing wrong?
 

Neil Whitworth

Dabbler
Joined
Nov 14, 2013
Messages
30
I don't think you are doing anything wrong. I think you need to use
Code:
ps -x
to show the background daemon. See the ps man page for more options.
 

Freddy500

Cadet
Joined
Jan 29, 2014
Messages
6
Ok, I got the script to run as a daemon at boot. Still facing two problems:
  • It does not seem to shut itself down properly when doing a shutdown. When I boot up again both when rc.local or me manually try to start the script I get the message that /tmp/autoshutdown.pid already exists and the script doesn't get launched. I then need to manually stop the script and start it again for it to work. I've set the following twice in the script both at the top of the script and under the class AutoShutdownDaemon(Daemon):
Code:
shutdownAfterInactivitySeconds = 240


  • The script doesn't seem to detect when a share is mounted both in the case of an afpd share to a Macbook (which would be the primary usage) and a smb share to a pc. Even though the shares are mounted and accessed it still shuts down.
 

seibax

Dabbler
Joined
Feb 25, 2014
Messages
17
Hi,

how can i make this script autostart at boot time??? It works fine when i start it by hand...

Thanks for any info...

  1. # Desired idle interval in seconds
  2. interval=240
  3. while [ true ]; do
  4. if zpool iostat $interval 2 | tail +1 |tail +`zpool iostat |wc -l|sed "s/ *//;"`|grep -q "0 *0 *0 *$"; then
  5. shutdown -p now
  6. fi
  7. done
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
See the manual regarding cronjobs...
 

seibax

Dabbler
Joined
Feb 25, 2014
Messages
17
hi cyberjock,

i cannot find something about launching the script after boot? Cron is for scheduled processes i think??
 

gpsguy

Active Member
Joined
Jan 22, 2012
Messages
4,472
From the 9.1.1 manual ...

"FreeNAS® 9.1.0 added the ability to schedule commands or scripts to run at system startup or
shutdown.

Figure 4.2a shows the screen that opens when you click System → Init/Shutdown Scripts → Add
Init/Shutdown Script."
 

seibax

Dabbler
Joined
Feb 25, 2014
Messages
17
Works like a charm
Thank you very much


Gesendet von meinem iPad mit Tapatalk HD
 

unca_NAS

Explorer
Joined
Mar 25, 2012
Messages
87
Thank you for this, propably got this working :)
Didnt know that Python has this strict policy concerning the indentions.. you learn something new every day (and usually forget something too in the process...)
 
Status
Not open for further replies.
Top