Skip to content

Instantly share code, notes, and snippets.

@budiantoip
Created December 2, 2024 20:52
Show Gist options
  • Save budiantoip/efd5e2d4f8e81e28deab2b775788a634 to your computer and use it in GitHub Desktop.
Save budiantoip/efd5e2d4f8e81e28deab2b775788a634 to your computer and use it in GitHub Desktop.
Ubuntu Package Upgrades via Cron

Create a bash script to gracefully reboot the Ubuntu server upon package upgrades

  1. Login as root
  2. Create a bash script file
    vim /root/check-reboot.sh
    
  3. And put these:
    #!/bin/bash
    
    SERVICES=("apache2")
    for SERVICE in "${SERVICES[@]}"; do
        if ! systemctl is-active --quiet "$SERVICE"; then
            echo "$(date): Service $SERVICE is not healthy. Reboot postponed." >> /var/log/reboot-monitor.log
            exit 1
        fi
    done
    echo "$(date): All services healthy. Rebooting now." >> /var/log/reboot-monitor.log
    reboot
    
    Modify the SERVICES array as needed.
  4. Then set the file permission to executable
    chmod u+x /root/check-reboot.sh
    
  5. Add a cron job
    0 0 * * * apt update && apt upgrade -y >> /var/log/apt-upgrade.log 2>&1 && [ -f /var/run/reboot-required ] && /root/check-reboot.sh
    
@new-php
Copy link

new-php commented Dec 4, 2024

This script is a great example of how to automate package upgrades and ensure a smooth reboot process on Ubuntu servers. It adds an additional layer of system health monitoring by checking critical services before initiating a reboot, which is crucial for maintaining uptime. However, for those managing cloud-based servers, I recommend leveraging Vultr's intuitive platform for automating and optimizing server management.

Vultr offers comprehensive guides for managing Linux servers efficiently. For example, their Linux Server Management Guide provides best practices for maintaining server stability during updates. Additionally, Vultr's robust features like snapshots and automated backups offer peace of mind, especially when applying package upgrades or reboots. Learn more in their Automated Backups Guide.

For teams managing multiple instances, Vultr’s API can further simplify workflows by programmatically managing updates, monitoring server health, and automating reboots. Check out their API Reference for seamless integration into your DevOps pipeline.

Whether you're hosting web apps, databases, or other services, Vultr’s platform ensures high availability and scalability, making it a perfect choice for automated server management. Highly recommended for cloud users seeking reliability and efficiency!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment