Skip to main content

Auto Update - Upgrade a Linux machine

Download Script os-update.sh

🚀 One-Line Download & Execute:

The below script is to auto update - upgrade - remove - clean and auto add a cronjob at Linux machine or Server.

apt update && apt install -y curl && clear && curl -fsSL https://docs.greenhome.stream/attachments/3 -o os-update.sh && chmod +x os-update.sh && clear && ./os-update.sh
Creating the script

Open the terminal and type the below command :

nano os-update.sh

and then copy / paste the below code inside the file "os-update.sh"

#!/bin/bash

# Clear the terminal for cleanliness
clear

# Function to handle errors
handle_error() {
    echo "Error occurred while executing: $1"
    exit 1
}

# Function to add the cron job
add_cron_job() {
    # Define the cron job
    cron_job="0 9 * * 6 /root/os-update.sh"
    
    # Check if the cron job already exists
    crontab -l | grep -q "$cron_job"
    if [ $? -ne 0 ]; then
        # If not, add it to the crontab
        (crontab -l; echo "$cron_job") | crontab -
        if [ $? -eq 0 ]; then
            echo "Cron job added successfully: '$cron_job'"
        else
            echo "Failed to add the cron job."
            exit 1
        fi
    fi
}

echo "============================================"
echo "       Automatic System Maintenance         "
echo "============================================"

# Get updates
echo "Step 1: Getting Updates..."
echo "--------------------------------------------"
sleep 1
apt-get update || handle_error "apt-get update"

echo "________________________________________________"
sleep 3

# Installing updates
echo "Step 2: Installing Updates..."
echo "--------------------------------------------"
sleep 1
apt-get dist-upgrade -y || handle_error "apt-get dist-upgrade"

echo "________________________________________________"
sleep 3

# Auto remove unnecessary packages
echo "Step 3: Auto Removing Unused Packages..."
echo "--------------------------------------------"
sleep 1
apt-get autoremove -y || handle_error "apt-get autoremove"

echo "________________________________________________"
sleep 3

# Clean up old package files
echo "Step 4: Cleaning Up Unused Package Files..."
echo "--------------------------------------------"
sleep 1
# Performing clean and autoclean
apt-get clean || handle_error "apt-get clean"
apt-get autoclean || handle_error "apt-get autoclean"

echo "________________________________________________"
sleep 3

# Add Cron Job before reboot
add_cron_job

# Prepare for reboot
clear
echo "============================================"
echo "           System Maintenance Complete      "
echo "============================================"
echo "System will REBOOT in 30 seconds..."
sleep 30

# Reboot the system
reboot

then press Ctrl + x it will prompt you [ Save modified buffer? ] press  y  and enter

Make the script executable

After that you need to make the script executable, so you will write the below command : 

chmod +x os-update.sh
Add the script to cron job to run automatically

To run the script daily follow the below steps, you can change according to your need.

crontab -e

then go at the end of the file and add the below line, change the  your_path  with your folder where the script is locate.

@daily /your_path/os-update.sh

script-os-update.jpg

then press Ctrl + x it will prompt you [ Save modified buffer? ] press  y  and enter

You are done the script in the location  /your_path/os-update.sh  it will automatically run daily.

Download the script os-update.sh

Green.