Managing a web server sometimes requires restarting Apache HTTPD for maintenance or operational purposes. Instead of performing the process manually, you can automate scheduled restarts and receive notifications through Discord.
In this tutorial, I’ll show you how to use a simple Bash script, cron, and Discord Webhooks to automate Apache HTTPD restarts on a cPanel server.
GitHub Repository: alerting-auto-restart-webserver-httpd
What Does This Script Do?
The script automates a scheduled Apache restart and sends three Discord notifications:
- Before restart: Alerts you one minute before the operation.
- Restarting: Notifies you when the restart process begins.
- After restart: Reports whether the restart command succeeded or failed.
Keep in mind that this is a scheduled restart script, not a continuous monitoring system that automatically detects when Apache goes down.
Requirements
Before getting started, make sure you have:
- A cPanel server running Apache HTTP Server.
- SSH access with sufficient administrative privileges.
- Bash and
curlinstalled. - A Discord Webhook URL.
- Cron available for scheduling the script.
The script uses /scripts/restartsrv_httpd --hard, a cPanel-specific command. It may interrupt active connections, so schedule it during an appropriate maintenance window.
Step 1: Download the Script
Connect to your server through SSH and clone the repository:
git clone https://github.com/aldinowildhanku/alerting-auto-restart-webserver-httpd.git
cd alerting-auto-restart-webserver-httpd
chmod +x autorestartapache.sh
Step 2: Configure Discord Notifications
Create a webhook in your Discord channel settings. Copy its URL, then open the script:
nano autorestartapache.sh
Find the webhook configuration and replace the placeholder with your actual URL:
WEBHOOK_URL="YOUR_DISCORD_WEBHOOK_URL"
Save the file after updating the configuration.
Security tip: Never publish your real webhook URL on GitHub. Anyone with access to it may be able to send messages to your Discord channel.
Step 3: Test the Script
Run the script manually:
./autorestartapache.sh
The script sends a warning, waits 60 seconds, announces the restart, executes the cPanel restart command, and sends a final status notification.
Test it during a maintenance window because it performs an actual Apache restart.
You can check for basic Bash syntax errors without running the restart:
bash -n autorestartapache.sh
Step 4: Schedule the Restart with Cron
After testing, open the root user’s crontab:
sudo crontab -e
Add the following example to schedule a restart every Sunday at 3:00 AM:
0 3 * * 0 /root/alerting-auto-restart-webserver-httpd/autorestartapache.sh
Replace the script path with its actual location on your server. The schedule follows the server’s cron timezone.
Save the crontab and verify the entry:
sudo crontab -l
Choose a maintenance schedule appropriate for your workload. Not every Apache server needs a routine restart, and unnecessary restarts can interrupt active users.
Troubleshooting
Discord notifications are missing
Check your webhook URL, curl installation, and outbound HTTPS connectivity. The original script suppresses webhook request errors, so a restart notification may fail even if the restart itself runs.
The cron job does not run
Verify the script path, executable permissions, cron service, and server timezone. Review the server’s cron logs for errors.
The restart command fails
Confirm that your server runs a compatible cPanel environment and that /scripts/restartsrv_httpd is available. Review your server logs to investigate the cause.
Conclusion
With Bash, cron, and Discord Webhooks, you can automate scheduled Apache HTTPD restarts on cPanel and receive notifications before, during, and after the operation.
This lightweight script is useful for planned maintenance, but it does not replace service monitoring or post-restart website health checks.
Explore the source code and updates here:


