Creating a script to schedule Ubuntu software upgrades
Step-by-Step Implementation
- Create the Upgrade Script
- Open your terminal and create a new file named upgrade_script.sh using your preferred text editor (e.g., nano upgrade_script.sh).
- Insert the following bash script content:
#!/bin/bash
# --- Script to Schedule Ubuntu Software Upgrades ---
# This script updates, upgrades, and cleans your Ubuntu system.
# It then prompts the user for the frequency of scheduling this script using cron.
# Function to perform the upgrade process
perform_upgrade() {
echo "Starting Ubuntu software upgrade..."
# Update package lists
sudo apt update -y
# Upgrade packages (installs newer versions)
sudo apt upgrade -y
# Remove unnecessary packages
sudo apt autoremove -y
# Clean up downloaded package archives
sudo apt clean -y
echo "Ubuntu software upgrade completed."
}
# Ask the user for the scheduling frequency
echo "How often would you like to schedule the Ubuntu software upgrade? (daily, weekly, monthly)"
read schedule_frequency
# Validate user input and set cron schedule
case "$schedule_frequency" in
"daily")
cron_schedule="0 2 * * *" # Run daily at 2:00 AM
;;
"weekly")
cron_schedule="0 2 * * 0" # Run weekly on Sunday at 2:00 AM
;;
"monthly")
cron_schedule="0 2 1 * *" # Run monthly on the 1st at 2:00 AM
;;
*)
echo "Invalid frequency entered. Defaulting to weekly."
cron_schedule="0 2 * * 0" # Default to weekly
;;
esac
# Create or modify the crontab entry
(crontab -l 2>/dev/null; echo "$cron_schedule /path/to/your/upgrade_script.sh") | crontab -
echo "Upgrade script scheduled to run $schedule_frequency."
- Make the Script Executable
Save the file and make it executable by running the command: chmod +x upgrade_script.sh. - Run the Script
- Execute the script with root privileges: sudo ./upgrade_script.sh.
- The script will perform the initial upgrade, ask for your preferred scheduling frequency (daily, weekly, or monthly), and set up the cron job accordingly.
Comments