7

Possible Duplicate:
Centos - Automatically issue a command after rebooting

I'd like to configure a server (Ubuntu) to send me an email whenever it reboots.

What would be the best way to do this?

Alf Eaton
  • 203
  • 1
  • 2
  • 5

4 Answers4

11

First ensure that you have sendmail installed on your server. You can install this by running tasksel and selecting the predefined "Mail server" collection.

Once this is complete, edit your root crontab (sudo crontab -e) and insert this line:

@reboot mail -s "$(hostname) was rebooted at: `date +%m.%d.%Y.%H.%M`" youremail@gmail.com

The -s switch sets the subject of the email and the date command will insert the hour and time at which the server rebooted.

Evan
  • 308
  • 1
  • 7
  • 1
    I went with this in the end, using `mail` from `mailutils`: echo "Your server was rebooted at: `date +'%Y-%m-%d %H:%M'`" | mail -s "[reboot]" youremail@gmail.com – Alf Eaton Mar 13 '12 at 16:22
  • will the way the system is rebooted have an impact on if this crontab is run or will it run every time the system boots? – Matt Mar 13 '17 at 17:03
  • 1
    would replace `Your server` by `$(hostname)` – Philippe Gachoud Dec 10 '18 at 15:45
3

I've found that running Monit on a server, when configured to do so, lets me know every time it is restarted. Investigation follows.

Plus doing it this way adds all the monitoring functionality that helps with other utilities and troubleshooting. The reboot notification is just a bonus; if you're running services on the server, or want a quick glance at the health of the server, this (or a similar package) is a good approach. At least it has been for us...

Bart Silverstrim
  • 31,092
  • 9
  • 65
  • 87
3

/etc/rc.local is supposed to be run on boot. You can call a script from there. Just make sure the exit code from your script is 0.

cjc
  • 24,533
  • 2
  • 49
  • 69
0

Put a correctly configuredsendcommand in a script then put that in the/etc/init.d/directory. Then make it executable with a chmod +x and run the following;

update-rc.d whateveryourscriptiscalled defaults

Chopper3
  • 100,240
  • 9
  • 106
  • 238