{"id":552,"date":"2012-07-10T10:57:50","date_gmt":"2012-07-10T02:57:50","guid":{"rendered":"http:\/\/rmohan.com\/?p=552"},"modified":"2012-08-30T15:26:10","modified_gmt":"2012-08-30T07:26:10","slug":"email-on-shutdown-and-restart-linux","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=552","title":{"rendered":"Email on shutdown and restart (Linux)"},"content":{"rendered":"<p>&nbsp;<\/p>\n<div>\n<p>The ability to know when a system shutdown (gracefully) and when it came back up can be invaluable to a System Administrator. The tips below will send out an email when a Linux system is gracefully shutdown and again when the system has restarted. This means for a single reboot the Administrator will receive two emails. This solution will not send out an email if the system loses power, however an email will still be sent on reboot. This should indicate to the Administrator that something went wrong with the shutdown. The tips below are for a Red Hat based system but should be similar in other architectures.<\/p>\n<p><strong>Start Up Email (Easy Way)<br \/> <\/strong><\/p>\n<p>First up is the blurb that will send an email on system start only. This is a simple crontab entry, using one of the special time specifications that cron provides.<\/p>\n<div>\n<div id=\"highlighter_863697\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>crontab -e<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Hit the \u2018i\u2019 button and on the first empty line paste the following to send an email to example@example.com on system start up (replace with your address):<\/p>\n<div>\n<div id=\"highlighter_270186\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>@reboot\u00a0 echo \"Server has restarted \"`hostname` | mail -s \"System Restart\" example@example.com<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Hit the escape button and then type<\/p>\n<div>\n<div id=\"highlighter_226565\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\u00a0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Now restart the system. After a few minutes you should recieve an email saying your system restarted successfully. Nice and easy, but it doesn\u2019t quiet do what we want: send an email on start up AND shutdown. That brings us to:<\/p>\n<p><strong>Start Up and Shutdown Email (More advanced)<\/strong><\/p>\n<p>To get an email at both start up and shut down we need to write an init script. The tips below are specific to a Red Hat based system (Red Hat, Fedora, CentOS, etc) but should be fairly similar to others.<\/p>\n<p>First thing we need is to create a new script (the example below uses nano, but vi, emacs or any other editor can be used to do the same thing). The following is the complete script used in this example \u2013 explanations will follow. In your home directory (or root\u2019s home directory, at the end of this you want this script to be owned by root \u2013 this Tutorial does not cover permissions), type:<\/p>\n<div>\n<div id=\"highlighter_443925\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>nano SystemEmail<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Paste the following code into that document and save it.<\/p>\n<div>\n<div id=\"highlighter_447430\">\n<div>\u00a0<\/div>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<div>6<\/div>\n<div>7<\/div>\n<div>8<\/div>\n<div>9<\/div>\n<div>10<\/div>\n<div>11<\/div>\n<div>12<\/div>\n<div>13<\/div>\n<div>14<\/div>\n<div>15<\/div>\n<div>16<\/div>\n<div>17<\/div>\n<div>18<\/div>\n<div>19<\/div>\n<div>20<\/div>\n<div>21<\/div>\n<div>22<\/div>\n<div>23<\/div>\n<div>24<\/div>\n<div>25<\/div>\n<div>26<\/div>\n<div>27<\/div>\n<div>28<\/div>\n<div>29<\/div>\n<div>30<\/div>\n<div>31<\/div>\n<div>32<\/div>\n<div>33<\/div>\n<div>34<\/div>\n<div>35<\/div>\n<div>36<\/div>\n<div>37<\/div>\n<div>38<\/div>\n<div>39<\/div>\n<div>40<\/div>\n<div>41<\/div>\n<div>42<\/div>\n<div>43<\/div>\n<div>44<\/div>\n<div>45<\/div>\n<div>46<\/div>\n<div>47<\/div>\n<div>48<\/div>\n<div>49<\/div>\n<div>50<\/div>\n<div>51<\/div>\n<div>52<\/div>\n<div>53<\/div>\n<div>54<\/div>\n<div>55<\/div>\n<div>56<\/div>\n<div>57<\/div>\n<div>58<\/div>\n<div>59<\/div>\n<div>60<\/div>\n<div>61<\/div>\n<div>62<\/div>\n<div>63<\/div>\n<div>64<\/div>\n<div>65<\/div>\n<div>66<\/div>\n<div>67<\/div>\n<div>68<\/div>\n<div>69<\/div>\n<div>70<\/div>\n<\/td>\n<td>\n<div>\n<div><code>#!\/bin\/sh<\/code><\/div>\n<div><code># chkconfig: 2345 99 01<\/code><\/div>\n<div><code># Description: Sends an email at system start and shutdown<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>#############################################<\/code><\/div>\n<div><code>#\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #<\/code><\/div>\n<div><code># Send an email on system start\/stop to\u00a0\u00a0\u00a0\u00a0 #<\/code><\/div>\n<div><code># a user.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #<\/code><\/div>\n<div><code>#\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #<\/code><\/div>\n<div><code>#############################################<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>EMAIL=\"example@example.com\"<\/code><\/div>\n<div><code>RESTARTSUBJECT=\"[\"`hostname`\"] - System Startup\"<\/code><\/div>\n<div><code>SHUTDOWNSUBJECT=\"[\"`hostname`\"] - System Shutdown\"<\/code><\/div>\n<div><code>RESTARTBODY=\"This is an automated message to notify you that \"`hostname`\" started successfully.<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>Start up Date and Time: \"`date`<\/code><\/div>\n<div><code>SHUTDOWNBODY=\"This is an automated message to notify you that \"`hostname`\" is shutting down.<\/code><\/div>\n<div><code>Shutdown Date and Time: \"`date`<\/code><\/div>\n<div><code>LOCKFILE=\/var\/lock\/subsys\/SystemEmail<\/code><\/div>\n<div><code>RETVAL=0<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code># Source function library.<\/code><\/div>\n<div><code>. \/etc\/init.d\/functions<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>stop()<\/code><\/div>\n<div><code>{<\/code><\/div>\n<div><code>echo -n $\"Sending Shutdown Email: \"<\/code><\/div>\n<div><code>echo \"${SHUTDOWNBODY}\" | mutt -s \"${SHUTDOWNSUBJECT}\" ${EMAIL}<\/code><\/div>\n<div><code>RETVAL=$?<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>if [ ${RETVAL} -eq 0 ]; then<\/code><\/div>\n<div><code>rm -f ${LOCKFILE}<\/code><\/div>\n<div><code>success<\/code><\/div>\n<div><code>else<\/code><\/div>\n<div><code>failure<\/code><\/div>\n<div><code>fi<\/code><\/div>\n<div><code>echo<\/code><\/div>\n<div><code>return ${RETVAL}<\/code><\/div>\n<div><code>}<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>start()<\/code><\/div>\n<div><code>{<\/code><\/div>\n<div><code>echo -n $\"Sending Startup Email: \"<\/code><\/div>\n<div><code>echo \"${RESTARTBODY}\" | mutt -s \"${RESTARTSUBJECT}\" ${EMAIL}<\/code><\/div>\n<div><code>RETVAL=$?<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>if [ ${RETVAL} -eq 0 ]; then<\/code><\/div>\n<div><code>touch ${LOCKFILE}<\/code><\/div>\n<div><code>success<\/code><\/div>\n<div><code>else<\/code><\/div>\n<div><code>failure<\/code><\/div>\n<div><code>fi<\/code><\/div>\n<div><code>echo<\/code><\/div>\n<div><code>return ${RETVAL}<\/code><\/div>\n<div><code>}<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>case $1 in<\/code><\/div>\n<div><code>stop)<\/code><\/div>\n<div><code>stop<\/code><\/div>\n<div><code>;;<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>start)<\/code><\/div>\n<div><code>start<\/code><\/div>\n<div><code>;;<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>*)<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>esac<\/code><\/div>\n<div><code>exit ${RETVAL}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>The main chunk of the code is the <strong>case<\/strong> statement at the bottom. When this script is set up, it will automatically be passed either \u201cstart\u201d or \u201cstop\u201d as a parameter. Depending on that value, the <strong>case<\/strong> statement will either call the <strong>start()<\/strong> or <strong>stop()<\/strong> function. So, now that we have the script done, we need to set this up to run.<\/p>\n<p>First, we need to make it executable:<\/p>\n<div>\n<div id=\"highlighter_170052\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>chmod u+x SystemEmail<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>At this point you can test your code by running either of the two following commands. Both should send you an email, if you changed the appropriate variable in the script.<\/p>\n<div>\n<div id=\"highlighter_537057\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>.\/SystemEmail start<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<div>\n<div id=\"highlighter_433186\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>.\/SystemEmail stop<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Now we want to set this up to run at start up and shut down. Copy the script from your home directory to the <strong>init.d<\/strong> directory. Once it is here, you want the script to be owned by <strong>root<\/strong>. This will ensure that only the <strong>root<\/strong> user can make changes to the script. Since this will run everytime you start and stop your machine, this is a wise precaution.<\/p>\n<div>\n<div id=\"highlighter_195167\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>cp SystemEmail \/etc\/init.d\/<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Last thing we do is set this up to run automatically by configuring it via <strong>chkconfig<\/strong>.<\/p>\n<div>\n<div id=\"highlighter_712122\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>chkconfig --levels 3 SystemEmail on<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>You will now receive two emails during a normal system reboot. Congradulations.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp;<\/p>\n<p>The ability to know when a system shutdown (gracefully) and when it came back up can be invaluable to a System Administrator. The tips below will send out an email when a Linux system is gracefully shutdown and again when the system has restarted. This means for a single reboot the Administrator will receive [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/552"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=552"}],"version-history":[{"count":4,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/552\/revisions"}],"predecessor-version":[{"id":1221,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/552\/revisions\/1221"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}