Send email on shutdown or reboot

Status
Not open for further replies.

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
Is there a feature in the GUI to send an email on shutdown or reboot? I noticed that the UPS options do allow for sending an email, but I don't think it would trigger if I did a 'shutdown -h now' or 'reboot' command.

I'm just looking to have a FreeNAS server send a simple email of its impending shutdown or reboot when a shutdown or reboot is triggered for any reason. I'm thinking something along the lines of a small script that will execute as part of the shutdown process.

Any ideas how to do this? Or a pointer in the direction so I can figure it out on my own?

Thanks.

Edit: What about the opposite too. I'd like an email when a FreeNAS server is booting up.
 

Yell

Explorer
Joined
Oct 24, 2012
Messages
74
Use crontab should work like on every *nix system

Instead of the first five fields, one of eight special strings may
appear:

string meaning
------ -------
@reboot Run once, at startup.
@yearly Run once a year, "0 0 1 1 *".
@annually (same as @yearly)
@monthly Run once a month, "0 0 1 * *".
@weekly Run once a week, "0 0 * * 0".
@daily Run once a day, "0 0 * * *".
@midnight (same as @daily)
@hourly Run once an hour, "0 * * * *".


http://www.freebsd.org/cgi/man.cgi?query=crontab&sektion=5&apropos=0&manpath=FreeBSD+9.0-RELEASE
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
Ok, that covers the startup email, what about a shutdown email :)
 

BobCochran

Contributor
Joined
Aug 5, 2011
Messages
184
When you shut down a system, you stop your servers. One of these is the smtp server. Find the shutdown script for it and modify that script so that the last thing that happens before the server is shut down is to send you an email. I think you can filter on the signals that are sent to the server process which prompt shutdown, so that you can tailor the email for very specific types of shutdowns.
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
That's kind of what I was thinking. I just don't know what the shutdown script is called. :P I also wasn't sure if what I'm asking for is technically feasible. I'm considering putting in a ticket to have this feature added. I always send myself text messages from my FreeNAS servers. For instance, I use Verizon, so an email to myphonenumber@vtext.com will go to my phone as a text. All of the major cell phone providers in the USA offer this service. Pretty fancy stuff, except the nightly texts at 3am when I'm trying to sleep.
 

BobCochran

Contributor
Joined
Aug 5, 2011
Messages
184
For example I think there are scripts in /etc/rc.d which provide generic startup and shutdown actions for most daemons. One would be for the mail server. I think you can easily change the script and get something to work. A larger challenge might be changing FreeNAS startup and shutdown scripts so that they persist across reboots. Perhaps you can develop plugins that will do what you need and which can be installed quickly across FreeNAS version upgrades, too.
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
Ooh. I'll check it out! I could easily do some email from Windows, but FreeBSD is one of my "pet projects" right now. I'm really enjoying getting to know FreeBSD, my limitation is knowing when I want to do something is over my head. Last time I tried a pet project I learned how to recompile the FreeNAS kernel. LOL!
 

BobCochran

Contributor
Joined
Aug 5, 2011
Messages
184
It is not that hard. Look at the documentation. I used to do this a bit with sendmail but forgot how. Look at how FreeNAS uses a cron job to send you emails each evening telling you the health of your system, and modify that for your own needs but not as a cron job. Rather, as part of the script the shuts down the mail server. Also look at the documentation for the smtp server. I seem to remember they tell you how to send yourself a quick email to test connectivity. The FreeNAS GUI does exactly that within its settings panels. I think you'll find it quite easy.
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
So I got a script to send me an email. Per reading FreeBSD manuals all the stuff in /etc/rc.d is executed. So I added the script and when I reboot the script is gone.

I did the 'mount -uw /' and although I've tried twice the file disappears on reboot. Any ideas? Is /etc protected some other way?
 

Yell

Explorer
Joined
Oct 24, 2012
Messages
74

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
I submitted a ticket: https://support.freenas.org/ticket/1880

Right now I'm having problems saving anything to /etc. It seems to be read-only even when I use 'mount -uw /'. It's odd because I have used that command before for other modifications/tinkering but it appears that /etc is different.

Edit: my homemade script contains only 1 line:

Code:
mail -s "Startup commencing" myphonenumber@vtext.com < /mnt/Harddisk/startupemail.txt


I added a script file to /etc/rc.d and it runs fine when I manually execute it, but on reboot the damn file disappears and I have no clue why. I've tried several times and on each reboot the file disappears.

I haven't gotten to the shutdown part yet since I'm getting my butt kicked with /etc somehow being readonly even after I execute mount -uw /.
 

JaimieV

Guru
Joined
Oct 12, 2012
Messages
742
You need to make the changes in /conf/base/etc/, that gets copied to the (ramdisk) /etc/ at startup.
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
That was exactly it! Problem solved. Of course, I'm not getting the emails I thought I'd be getting, so more reading is in order.
 

JaimieV

Guru
Joined
Oct 12, 2012
Messages
742
Make sure you're sending them while the mail service is running!
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
I am.

During the shutdown I get an error that pops up, but I do receive the email subject(no body). I'm not sure what the error is because it flashes across the screen to fast. That's fine because I wanted to send the email with no body, but I couldn't figure out how to use the mail command and tell it I didn't need a body.

Edit: So startups are going to be a slight issue. I'll have to do more reading. I was trying to execute a script in /etc/rc.d but I'm not sure if it's executing before the mail daemon starts or if there's an error. Not sure where to look since the startup messages scroll so fast I can't read them.

Another Edit: Found it. I get the message "/etc/rc: WARNING: Ignoring old-style startup script /etc/fc.d/sendemail.sh" Guess I'll have to determine what the "new" style is.
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
So here's the status:

Startup: Added a line to /etc/crontab(really I edited /conf/base/etc/crontab):

Code:
@reboot root sh /mnt/Harddisk/startupemail.sh


I get a startup every single bootup. A cron job is probably the "cleanest" way to get the startup emails to work.

Shutdown: Added a line to /etc/rc.shutdown(really its /conf/base/etc/rc.shutdown):

Code:
/bin/sh /mnt/Harddisk/shutdownemail.sh


If I SSH in and use the command reboot then I get no shutdown email. But if I run shutdown -r now then I do get a shutdown message. If you watch the messages during a shutdown -r and reboot, they are different.

Both startupemail.sh and shutdownemail.sh have the same basic line:

mail -s "Startup Complete" myphonenumber@vtext.com < /mnt/Harddisk/startupemail.txt

I added a blank txt file since I couldn't figure out how to get an email sent with no body.
 

Yell

Explorer
Joined
Oct 24, 2012
Messages
74
Thats because they are (mostly) 2 different utility's for different purpose.


The shutdown utility provides an automated shutdown procedure for super-
users to nicely notify users when the system is shutting down
http://www.freebsd.org/cgi/man.cgi?query=shutdown&sektion=8

The halt and reboot utilities flush the file system cache to disk, send
all running processes a SIGTERM (and subsequently a SIGKILL) and, respec-
tively, halt or restart the system.
..
Normally, the shutdown(8) utility is used when the system needs to be
halted or restarted, giving users advance warning of their impending doom
and cleanly terminating specific programs.

http://www.freebsd.org/cgi/man.cgi?query=reboot&sektion=8

Miquel van Smoorenburg said:
"reboot" under SystemV traditionally does just that, it reboots the
system. Hard.

Due to historical BSD tradition and the first init for Linux being BSD-like
(remember simpleinit?) people expect "reboot" to do an orderly shutdown
and reboot.

So, the "reboot" command has to guess the context in which it is being
used, and then decide to do a hard reboot by calling the reboot(2)
system call, or to do an orderly shutdown. In the last case, it just
calls "shutdown -r now" for you!

It guesses that context by checking the runlevel (which is stored in
/var/run/utmp on a correctly running system). If it's "0" or "6",
reboot will assume it has to do a hard reboot. If it's "1" ... "5",
shutdown will be called. If it's anything else or reboot gets confused,
it prints a warning messages and calls shutdown.

Not sure whats freeNAS does. (my box is down, having some boot trouble and i'm to lazy)
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
WOW. That kind of confirms what I thought. I was thinking that reboot was more of a "reboot this shiznit right now!" and the shutdown -r now was more of a "do a nice graceful orderly shutdown".

I got a ticket in, and my server works with the steps I listed above. I just wish I knew how I could send a mail message from the command line without it asking for the body of the email. Then I could cleanup my txt files. I didn't see anything in the manpages on how to do it so I assume its not possible.
 

Yell

Explorer
Joined
Oct 24, 2012
Messages
74
tried the mighty pipeline?

Code:
echo "going down for cookies" | mail -s "Startup commencing" myphonenumber@vtext.com 


and if you just dont want a mail body use a nul

Code:
mail -s "Startup commencing" myphonenumber@vtext.com < /dev/null
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
tried the mighty pipeline?

Code:
echo "going down for cookies" | mail -s "Startup commencing" myphonenumber@vtext.com 


and if you just dont want a mail body use a nul

Code:
mail -s "Startup commencing" myphonenumber@vtext.com < /dev/null

And you got me there. I didn't even think of the pipeline. I didn't think null would work because I thought that mail required a body.

But the pipeline did work. So I guess I'll be fixing up my emails. Of course, I'm thinking that if I was the admin for multiple servers I'd want the email to include the server name. So I'll have to see about adding the server name as a variable to the command line. :) This is too much fun! Been up all night thanks to FreeBSD. Glad I'm not married or have kids. They'd be waking me up when i'm going to try to go to bed soon. :P
 
Status
Not open for further replies.
Top