Linux: How to Schedule a Download using a Cron Job

Nuwan Zen
2 min readJan 21, 2023

--

I wanted to download a large file using my night time internet data. Yes I live in a country where no unlimited data is provided.

I my requirement was to clone a git repository which is around 5GB.

Generally CronTab is nice tool available in any linux environment. You can check the current jobs using:

crontab -l
#output
20 1 21 * * /home/nuwan/Documents/TEMP/cron.sh

Create The Script

Let’s first create a script file to download the files and then add it as a cron job.

My folder location where files wanted to be downloaded was:

/home/nuwan/Documents/TEMP

Create a file named cron.sh with the following:

#!/bin/sh
cd /home/nuwan/Documents/TEMP
git clone https://github.com/hub-pre-prod.git

Note: please add a valid git repo here as mine is a private repo.

Go to file properties make the file executable.

You can test the script is running as expected by just executing the file as below, it should start the download, if its working you can cancel the download.

/home/nuwan/Documents/TEMP/cron.sh

Now Create A Cron Tab Entry

Go to crontab edit mode

 crontab -e

This command will open the file in an editor.

add the following as the last line

20 1 21 * *       /home/nuwan/Documents/TEMP/cron.sh

First part is cron expression and second part is the command to execute.

Let’s check what is in the cron expression

you can find many cron expression online creators for your customized requirements.

In my case what cron expression does is:

At 01:20 AM, on day 21 of the month

Verification

Next day morning I checked the disk and so the file has been created successfully.

pwd
/home/nuwan/Documents/TEMP

ls
hub-pre-prod cron.sh

Cleaning

As this cron expression is scheduled run monthly, this could run continuously. so make sure cron job is removed from the list once task is completed.

Before You Leave …

Subscribing to my stories would be awesome!

--

--

Nuwan Zen
Nuwan Zen

Written by Nuwan Zen

Sometimes A software Engineer, sometimes a support engineer, sometimes a devops engineer, sometimes a cloud engineer :D That’s how the this life goes!

No responses yet