How to automate updating package lists in Linux using cron jobs

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM


$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM


$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

To automate updating package lists using cron jobs on a Linux system, you can use the package manager’s update command (e.g., apt-get update for Debian-based systems). Here’s a step-by-step guide:

Create a script for updating package lists

Choose the text editor that you are most comfortable with. In this example, we’re using nano text editor, but you can use vim or emacs depending on your preference.

Since we’re using nano in this example, we’re going to create the file, using the following command;

sudo nano

The nano editor will create and open a file named In this file, you will need to add the following lines;


# Update package lists using apt-get (for Debian-based systems)
apt-get update
# below command will Upgrade the packages that can be upgraded
sudo apt upgrade -y
# below command will Remove unnecessary packages and dependencies for good memory management
sudo apt autoremove -y

# below command will Clean package cache
sudo apt clean -y

Now, you have saved your script in a file named in the current or any directory you’ve specified. You can run the script using the following command:

It’s important to note that while using the .sh extension is a convention, it’s not strictly required. The key is to choose a name that is meaningful and indicative of the file’s purpose. If your script is intended to be run as a Bash script, adding the .sh extension is a clear and widely recognized way to convey the purpose you want it to serve.

Add executable permission

Adding execute permissions to a script makes it possible to run the script directly without explicitly calling the bash interpreter. Without execute permissions, the cron tasks would need to run the script by specifying the interpreter, like this:


But once the execute permissions are added, the script can be run directly


To add execute permissions to your script, you use the chmod command with the +x option:

chmod +x

This command grants execute permissions to the owner of the file. After executing this command, you can run the script by simply typing its name preceded by .

Adding execute permissions is a security measure to ensure that you intentionally run the script. It prevents accidental execution and helps control who can run the script. Keep in mind that you should only grant execute permissions to scripts that you trust, especially if they contain system-level commands.

Test the script

Before you proceed, you will need to test if the script is configured properly by running it manually using the following command;


Now, confirm that the package lists are updated.

Schedule the script with a cron job

In this article, implementing a cron job will provide us with the automation magic we’re looking for so the process of updating the package list on our Linux system is done automatically and repetitively based on the time interval we can specify.

If you looking to learn more about Cron Jobs in Linux systems, we’ve written a detailed article on this topic, and you can read it here

Open your crontab file using the crontab -e command;

crontab -e

Add a line to schedule the script. For example, to run the script every day at 4:00 AM:

0 4 * * * /path/to/

Save the changes and exit the editor.

This cron job will execute the script at 4:00 AM every day, ensuring that the package lists are regularly updated. Adjust the paths and schedule according to your requirements.

RECOMMENDED READING: What are Cron Jobs and how to use them in Linux Systems?

Redirect the output

Consider redirecting the output of the command to a log file to track the updates:

0 4 * * * /path/to/ >> /var/log/update_packages.log 2>&1

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.