Send email on shutdown or reboot

Status
Not open for further replies.

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
My new shutdown command:

echo "$HOST at IP $REMOTEHOST is shutting down." | mail -s "SHUTDOWN IN PROGRESS" myphonenumber@vtext.com

My new startup command:

echo "$HOST at IP $REMOTEHOST is online." |mail -s "STARTUP COMPLETE" myphonenumber@vtext.com

Piece of cake! Too much fun!
 

BobCochran

Contributor
Joined
Aug 5, 2011
Messages
184
Are the changes made to /conf/base/etc/rc.d saved in the config?
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
They are if you run mount -uw / first. The changes I listed are persistent across reboots and I get an email every time I shutdown and startup the server. :)

Edit: When you say "config" do you mean are the changes kept in the FreeNAS DB? No. These are all manual changes and more than likely would disappear if you upgraded the OS.
 

Osiris

Contributor
Joined
Aug 15, 2013
Messages
148
Just posting my startup script ... didn't find anything as simple as this outthere.
Works on FreeNas 9.1.0
It requires your smtp server configured in freenas settings, and your root mail account.
you can replace the 'root' by "$TO" at the bottom, obviously.


PHP:
#!/bin/bash
FROM="freenas3@yourdomain.com"
TO="yourmail@yourdomain.com"
SUBJECT="freenas3 startup"
BODY=""
SENDEMAIL=1
ERROR=0
SEP=" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "
ZPOOL=/sbin/zpool
PRINTF=/usr/bin/printf
 
function _log {
    DATE="`date +"%Y-%m-%d %H:%M:%S"`"
    BODY="${BODY}$DATE:\n$1n"
}
 
# find all pools
mypools=$($ZPOOL list -H -o name)
 
for pool in $mypools; do
    _log "`$ZPOOL status -v $pool`"
    _log "$SEP"
    if ! ( $ZPOOL status -v $pool | grep -q "No known data errors" ); then
        _log "data errors detected on $pool"
        ERROR=1
    fi
    if ( $ZPOOL status -v $pool | grep -q "DEGRADED" ); then
        _log "data errors detected on $pool"
        ERROR=1
    fi
done
 
# change e-mail subject if there was error
if [ $ERROR = 1 ]; then
    SUBJECT="${SUBJECT}: Error(s) found"
fi
 
# send e-mail
if [ $SENDEMAIL = 1 ]; then
    $PRINTF "$BODY" | mail -s "$SUBJECT" root
fi
 

Osiris

Contributor
Joined
Aug 15, 2013
Messages
148
btw, I've put that file in / and chmod +x'd it.
it's running as a post-init command : /startupcheck.sh
I don't know how to use the script syntax.
 
Status
Not open for further replies.
Top