Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have an Ubuntu 10.04 desktop at home that runs 24/7. Recently, I've noticed that the machine occasionally restarts on its own (approx once per month). I would like to set this server up so that I get an email every time the server starts. I would like to get the email at my gmail account and I'll set up another gmail account for the server to use to send emails (if necessary).

As an added bonus, it would be great if the email could contain some sort of diagnostic information pertaining to what caused the server to crash and restart.

Thanks

share|improve this question
    
For actually sending the email with sendmail, check out appgirl.net/blog/2009/… –  cpast Jan 1 '13 at 20:04

2 Answers 2

It's hard to track why your computer crashed and restarted, but you can e-mail your logs with the notification e-mail. For advanced monitoring, I suggest trying out Zabbix, NagiOS or Cacti. However, here's a simple Python 2.7 script for sending an e-mail with a command "./script.py username_for_gmail path_to_letter":

#!/usr/bin/python2.7
import smtplib
import getopt
import sys

args = getopt.getopt(sys.argv[1:], ":")
laiskas = args[1][1]
SENDER = "no-reply@" + args[1][0]
mssg = open(laiskas, 'r').read()
RECIPIENTS = ['ernetas@gmail.com']

server = smtplib.SMTP('smtp.gmail.com',587)
#server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.ehlo()
server.login(args[1][0] + "@gmail.com","R7olM6Qw")
server.sendmail(SENDER,RECIPIENTS,mssg)
server.quit()

You have to change R7olM6Qw to your own GMail password. Also, please change RECIPIENTS variable for your addresses. The script needs smtplib and python2.7, which may have to be installed on your server from apt.

Then you can create a simple text file next to the script:

To: recipient@gmail.com
From: same_address@gmail.com
Subject: Computer powered on!
Random text here...

In the end, just add a new line to the /etc/rc.local file:

/path/to/the/python/script gmail_username /path/to/the/letter

However, this is not a safe method as you will be storing your password insecurely, so I suggest creating a mailbox just for that. Also, please, mind it and chmod all scripts in order to prevent other users doing anything with them.

Instead, I think that it is rather more reasonable to have a separate server with PHP mailer script available only for servers' IPs. In that case, the separate server would be doing the mailing service securely, while leaving your other servers only with curl or wget for pushing the notifications.

share|improve this answer

I see at least two possible options:

  1. Add a new cronjob (e.g. @reboot root mail -s "I just rebooted" your@mail.tld
  2. Install logcheck; it will send you the log messages containing the reason for the reboot (if there are any log entries about them)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.