When an essential Linux server reboots—whether due to a scheduled kernel update, a power failure, or a system crash—system administrators need to know immediately. While email alerts are standard, they are often buried in crowded inboxes. By utilizing the @reboot directive in cron, you can configure your Ubuntu server to automatically push a real-time alert directly to your team’s Slack channel the exact moment the server comes back online.
How to Create a Slack Incoming Webhook
Before touching the Linux server, you must generate a secure Webhook URL from Slack. This URL acts as a dedicated endpoint that allows your server to POST messages to a specific channel without needing full API authentication.
- Log into your Slack workspace in a web browser.
- Navigate to api.slack.com/apps and click Create New App.
- Select From scratch, name it “Server Alerts,” and assign it to your workspace.
- In the left-hand menu, click on Incoming Webhooks and toggle the feature to On.
- Scroll down and click Add New Webhook to Workspace.
- Select the specific channel (e.g.,
#devops-alerts) where the notifications should appear and click Allow. - Copy the generated Webhook URL (it will begin with
https://hooks.slack.com/...). Keep this URL secret.
How to Configure the Cron Job in Ubuntu
Now, we will use the standard crontab utility on the Ubuntu server to fire a curl command containing a JSON payload to your new Slack Webhook every time the machine boots.
- SSH into your Ubuntu server.
- Open the root crontab file by running:
sudo crontab -e - If prompted, select your preferred text editor (e.g., nano).
- Scroll to the very bottom of the file and paste the following line, ensuring you replace the placeholder with your actual Webhook URL:
@reboot sleep 30 && curl -X POST -H 'Content-type: application/json' --data '{"text":"🚨 *ALERT*: Ubuntu Server ('$(hostname)') has just rebooted and is back online."}' https://hooks.slack.com/services/YOUR/WEBHOOK/URL - Save the file and exit the editor.
Why the Sleep Command is Necessary
The sleep 30 command is a critical component of the script. The @reboot directive triggers the moment the cron daemon starts, which is often before the server has fully initialized its network interfaces. If curl attempts to reach the Slack servers before the Ubuntu machine has established internet connectivity, the command will silently fail. Pausing the script for 30 seconds ensures the network stack is completely active before the POST request is sent.