I'm running ec2 instance with ubuntu server and sometimes it seems to be shut down. Is it possible to send an email notification, if server switches to runlevel 0? And how?
Probably a better alternative that will handle external shutdown cases (server crash, service disruption, kernel panic, etc) is to use Amazon's CloudWatch and Simple Notification Services to send you Alarms.
Just set your thresholds (in this case, probably INSUFFICIENT_DATA) and where you want the alert to go (in your case, probably email) and let Amazon handle the rest.
For linux boxes in general, find the default runlevel by doing a
and looking at the second of the colon-separated fields. It's usually 3 or 5, with the former more common in servers.
Assuming it's 3, look in
You will want to write something short and sweet and put it in there for early running, say
This will still only help you if the server shuts down gracefully, and if you're finding it shut down for no clear reason, that may not be so. If the availability of your box is important to you, I would personally recommend putting a small nagios installation on a cheap shared-hosting server elsewhere, and have that monitor your ec2 box.