Menu

Saturday, 1 November 2025

πŸ•’ Shell Scripting for Beginners – Part 9: Scheduling & Automation

  • 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

  • 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 2025

Example 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.sh
Build 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.sql

Example 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 MAILTO in 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