Helper script for automatic shutdown on ZFS idle

Status
Not open for further replies.

wilko16

Cadet
Joined
May 23, 2013
Messages
1
Hi, together,
as it seems as if this a request raised every now and then: Maybe following script can be handy. It monitors the ZFS pool and shuts down the system if a predefined number of seconds no IO activity is detected.
Actually, it's more a one-liner ;-)

---
#!/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
---

Comments appreciated.
Bye Wilko
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
Outstanding first post!

I think I might see if I can modify this to auto unmount an encrypted zpool and reload the services after so many minutes of inactivity. I know a business that was wanting it to auto-unmount after an hour of no zpool access. Hmm....
 

Caesar

Contributor
Joined
Feb 22, 2013
Messages
114
why would anyone want the server to auto shutdown? FreeNAS is slow booting up so I didn't think that it would make a good on demand service
 

gpsguy

Active Member
Joined
Jan 22, 2012
Messages
4,472
I can imagine cases where users might want to shut it down automatically. For example, someone might want to only use in the evening after work. And, save some money by not running it 24x7, if they don't need it.

As far as slow bootup time, that's relative. Maybe compared to an iPad, but even my HP N40L boots faster than other machines I've had in the past.
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
I wouldn't recommend a script for automatic shutdown typically just because hard drives seem to last longer being left running all the time. There are situations(like backup servers) where this could prove very useful. Power on the server and the server will auto-shutdown itself when the backup completes!
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
I'm having a heck of a time figuring out how to restart samba, nfs, and iscsi from the command line in FreeNAS. :(
 

titan_rw

Guru
Joined
Sep 1, 2012
Messages
586
Freenas seems to support what I've seen as RedHat style service commands. At least I saw them first in redhat. I'm not sure where they originated.

"service samba restart" seems to work.

Or the more general unix way:

/usr/local/etc/rc.d/samba restart

Other services should be the same, you'll just have to figure out what they're called.


In Debian, I'm used to "/etc/init.d/samba restart". It appears the "service" syntax works there too.
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
Ok, I got the restarting of services working. Just not sure how I want to implement this. I could do a cronjob at startup, but then it would terminate after the zpool is unmounted the first time.

I'm also trying to decide how to get geli to unmount the .geli devices without having to do individual lines for each drive.

I'm definitely having fun with this though.

Ok, I figured how to unmount the zpool. FreeNAS doesn't like me unmounting it, so I guess a reboot would be better than just unmounting the encrypted zpool.
 

Freddy500

Cadet
Joined
Jan 29, 2014
Messages
6
My apologies for reviving this older thread but I'm trying to get this to run under Ubuntu with zfsonlinux. When I execute the script I get a very long list of:

Code:
tail: cannot open ‘+1’ for reading: No such file or directory
tail: cannot open ‘+4’ for reading: No such file or directory
tail: cannot open ‘+1’ for reading: No such file or directory
tail: cannot open ‘+4’ for reading: No such file or directory
tail: cannot open ‘+1’ for reading: No such file or directory
tail: cannot open ‘+4’ for reading: No such file or directory

Anybody any idea what I can do?
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
That's good because that script wasn't meant for Linux. May 2013 there was no linux jail support on FreeNAS. In fact, FreeNAS 9.x hadn't even been released yet.
 

Freddy500

Cadet
Joined
Jan 29, 2014
Messages
6
Yes, I am aware of that. I got it to work perfectly well on FreeBSD so again thanks to wilko16 for this great script!

I was just hoping that the problem might be a simple syntax format adjustment for linux. It seems like zpool iostat is a zfs command that works under linux but it might be that the rest of the commands are too specific for bsd?

Edit:
I just gotten it to work under ubuntu! For anyone interested, the fault under linux seemed to be caused by how tail was called. I simply changed the + behind tail to - and now it works. I also changed the shutdown command from -p to -h:

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 -h now
fi
done


Not sure if this is a correct way to use tail but it seems to work for me.
 

serendipity

Cadet
Joined
Jun 20, 2015
Messages
6
I know this is an old thread, but it's quite unique and useful (to some).

Is there an easy way to check if there's a scrub in progress?

That output of "zpool status" will contain the phrase "scrub in progress". I presume it says similar if a resilver is in progress. However, checking "zpool status" doesn't seem to be necessary. The scrub will cause I/O activity which will get caught by the "zpool iostat" check.

Edit: just after posting this, I realised this thread continues on in spirit over at: https://forums.freenas.org/index.ph...tion-on-zfs-pool-inactivity-also-hello.35241/
 
Last edited:
Status
Not open for further replies.
Top