How to Automatically Send an Email Alert When a Linux Server Reboots Using Crontab

If you manage a fleet of headless Linux servers running critical applications, unexpected reboots are a nightmare. A kernel panic, a sudden hardware power failure, or a rogue automated update can cause a server to reboot in the middle of the night. If you do not have complex monitoring infrastructure (like Datadog or Prometheus) configured, you might not realize the server rebooted until angry customers complain that the application state was lost.

You do not need a massive enterprise monitoring suite to solve this. You can leverage the native Linux cron scheduler to automatically fire off a simple, lightweight email alert to your personal inbox the exact second the machine finishes booting up.

Prerequisites

For your server to send an email out to the actual internet, you must have a Mail Transfer Agent (MTA) installed and configured (like postfix or sendmail) or a simple mail utility like mailx configured to route through a secure SMTP relay (like SendGrid or AWS SES). This guide assumes the mail command already works on your server.

Step 1: Open the Root Crontab

Because you want this alert to fire regardless of which user is logged in, you should place the command in the global root crontab.

  1. Log into your server via SSH.
  2. Elevate your privileges and open the cron editor:
    sudo crontab -e
  3. If this is your first time running the command, Linux may ask you to choose a text editor. Select nano (usually option 1).

Step 2: The “@reboot” Magic String

Most sysadmins think cron is only for time-based scheduling (e.g., “Run this script every Tuesday at 4 AM”). However, cron supports a highly specific, hidden system event trigger called @reboot.

  1. Use the arrow keys to scroll to the very bottom of the file, past all the instructional text.
  2. Paste the following exact command (replace the email address with your actual email):
    @reboot echo "ALERT: The production database server (192.168.1.50) has just rebooted at $(date)." | mail -s "CRITICAL: Server Reboot Detected" [email protected]

Understanding the Command

  • @reboot: This commands the cron daemon to execute the subsequent instructions exactly once, the moment the operating system finishes its boot sequence.
  • echo "...": This generates the body of the email. By injecting the $(date) variable, the email will include the exact timestamp of the boot event.
  • | mail -s "...": This takes the text output and pipes it directly into the mail utility, attaching a highly visible subject line.

Step 3: Save and Test

  1. Press Ctrl+O then Enter to save the crontab file.
  2. Press Ctrl+X to exit the nano editor.
  3. You will see a message confirming crontab: installing new crontab.

To test your new early warning system, issue a reboot command to your server (sudo reboot). Approximately 30 seconds later, your phone will buzz with an email from your server, proving that your automated, zero-dependency monitoring system is fully operational.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.