- In DevOps and Middleware environments, automation isn't complete until it's scheduled.
- Here, we master cron, at, @reboot, and Jenkins integration — running scripts automatically for backups, restarts, monitoring, and cleanups.
π Table of Contents
- 1️⃣ Automating with Cron
- 2️⃣ One-Time Tasks with At
- 3️⃣ Auto-Execute on System Reboot
- 4️⃣ Jenkins Scheduler Integration
- 5️⃣ Real Middleware Automation Examples
- 6️⃣ Tips & Troubleshooting
- π Summary
⏰ 1️⃣ Automating with Cron
- cron executes scripts periodically.
- Perfect for recurring jobs like backups, cleanup, and health checks.
Example 1: Daily Jenkins Backup at 3 AM
0 3 * * * /opt/scripts/jenkins_backup.sh >> /var/log/jenkins_backup.log 2>&1
π¬ Output (log):
[03:00] Jenkins backup started[03:03] ✅ Jenkins backup completed
Example 2: Weekly Tomcat Log Cleanup
0 2 * * 0 /opt/scripts/cleanup_tomcat_logs.sh >> /var/log/tomcat_cleanup.log 2>&1
π¬ Output:
π§Ή Deleted old logs from /opt/tomcat/logs✅ Weekly cleanup done
π 2️⃣ One-Time Tasks with At
at executes a job once — ideal for maintenance windows, patches, or urgent restarts.
Example 3: Restart WebSphere at 11:30 PM
echo "/opt/IBM/WebSphere/AppServer/bin/stopServer.sh server1 && sleep 15 && /opt/IBM/WebSphere/AppServer/bin/startServer.sh server1" | at 23:30
π¬ Output:
job 17 at Sat Nov 1 23:30:00 2025Example 4: Run Backup Once in 2 Hours
echo "/opt/scripts/backup_db.sh" | at now + 2 hours
π¬ Output:
job 18 at Sat Nov 1 05:30:00 2025π 3️⃣ Auto-Execute on System Reboot
The @reboot keyword in crontab ensures your scripts run every time Linux boots.
Perfect for restarting services, mounting drives, or initializing environments.
Example 5: Auto-Start Tomcat After Reboot
@reboot /opt/tomcat/bin/startup.sh >> /var/log/tomcat_reboot.log 2>&1
π¬ Output (after reboot):
✅ Tomcat started automatically at boot.Example 6: Health Check Script at Boot
@reboot /opt/scripts/health_check.sh >> /var/log/health_boot.log 2>&1
π¬ Output (log):
[BOOT] Checking WebSphere, Jenkins, NGINX...✅ All services active.
π‘ To verify reboot jobs, check
grep CRON /var/log/syslog after restart.⚙️ 4️⃣ Jenkins Scheduler Integration
In CI/CD, Jenkins provides cron-like scheduling for build pipelines.
Example 7: Daily Jenkins Build at 2 AM
pipeline {
triggers { cron('H 2 * * *') }
stages {
stage('Nightly Build') {
steps {
sh '/opt/scripts/build_app.sh'
}
}
}
}
π¬ Output:
✅ Jenkins triggered build_app.shBuild completed successfully.
π§° 5️⃣ Real Middleware & DevOps Automation Examples
Example 8: NGINX Health Monitor Every 10 Minutes
*/10 * * * * /opt/scripts/nginx_health.sh >> /var/log/nginx_monitor.log 2>&1
π¬ Output:
[02:40] π NGINX is active[02:50] π¨ NGINX down – restarted automatically.
Example 9: Database Backup Every 6 Hours
0 */6 * * * mysqldump -u root -pSecret middlewaredb > /backup/db_$(date +%F_%H).sql
π¬ Output:
✅ Backup completed: db_2025-11-01_06.sqlExample 10: Restart Jenkins After Crash Detection
*/15 * * * * pgrep jenkins >/dev/null || systemctl restart jenkins
π¬ Output:
[03:15] π¨ Jenkins was down, restarted successfully.Example 11: Auto-Remount Missing Volume
@reboot mount | grep /mnt/backup >/dev/null || mount /dev/sdb1 /mnt/backup
π¬ Output:
✅ /mnt/backup auto-mounted on system startup.π§ 6️⃣ Tips & Troubleshooting
- π Check logs →
grep CRON /var/log/syslog - π Ensure permissions →
chmod +x /opt/scripts/*.sh - ⚙️ Test reboot jobs manually →
sudo run-parts /etc/cron.d/ - π§ Use
MAILTOin crontab for notifications - π¨ For Jenkins jobs, check
/var/lib/jenkins/jobs/*/builds
π Summary
- Cron → Schedules recurring jobs
- At → Runs one-time tasks
- @reboot → Executes scripts on startup
- Jenkins → Manages enterprise-level schedules
- π‘ Combine them to create self-healing, auto-starting Middleware environments.
No comments:
Post a Comment