How to Run Cron Jobs Every 5, 10, or 15 Minutes
Here's a quick example that shows how to run cron jobs every 5, 10 or 15 minutes in Linux.
In simple terms, a cron job allows you to do an operation at specific intervals such as every minute, day, week, etc.
But before I explain how you can run the cron jobs at specific intervals, it is necessary to know the syntax and available options.
Cron job syntax and operators explained
Crontab (cron table) is nothing but a text file where you can use the following syntax to add the task:
* * * * * command(s)
^ ^ ^ ^ ^
| | | | |
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
So if I want to create a cron job that is executed every 5 minutes, it would look like this:
*/5 * * * * command
But why did I use / before the 5?
Well, there are various operators that can be used when you specify the interval time. So here are the operators, characters, and symbols that are used with cronjobs:
*
: In any of the five fields, it indicated all the possible values. Such as an asterisk in the hour field will repeat the task every hour.-
: Using the hyphen, you can specify the range. For example, if you use the 1-3 in days field, it will repeat the task for the first 3 days in a week.,
: Using a comma, you can choose different intervals if you use 1,4,5 in the hour field, it will repeat the task at 1 AM, 4 AM, and 5 AM./
: It is used to divide a range of values. Such as if you use1-10/2
in the minute field, it means the action will be performed every 2 minutes withinL
: The letter L indicates Last. So if you use the L in the month field, the task will be executed at the end of every month.W
: The letter W is used to get the nearest weekday. For example, if you want the specific task to happen on the 1st of every month but if it comes on Saturday or Sunday, it will wait will the Monday.#
: The hash symbol is used to specify the day of the week.?
: The question mark is used when you don't want to specify the exact time. Can be useful in cases where you want to execute a task every month but don't care about the time of the execution.
Now, let's create a cronjob.
Create a cron job for every 5, 10, and 15 minutes
In this section, I will show you how you can create cron jobs with practical examples.
First, use the following command which will allow you to use the desired text editor to edit the cron jobs:
crontab -e
Here, I would recommend (and will be using) the nano:
Now, jump to the end of the file in the nano text editor and use any of the shown crown jobs for specific intervals.
For every 5 minutes (Checking internet speeds)
In this section, I will be using a script that will check the internet speed and if it falls down under 10 Mbps, the user will be notified through the notification.
Here's the script that I will be using:
#!/bin/bash
# Set the minimum download and upload speeds (in megabits per second)
MIN_DOWNLOAD_SPEED=50
MIN_UPLOAD_SPEED=10
# Run the speedtest and capture the output
OUTPUT=$(speedtest-cli --simple)
# Extract the download and upload speeds from the output
DOWNLOAD_SPEED=$(echo "$OUTPUT" | awk '/Download:/ {print $2}')
UPLOAD_SPEED=$(echo "$OUTPUT" | awk '/Upload:/ {print $2}')
# Convert the speeds from megabits per second to kilobits per second
DOWNLOAD_SPEED=$(echo "$DOWNLOAD_SPEED * 1000" | bc -l | awk -F. '{print $1}')
UPLOAD_SPEED=$(echo "$UPLOAD_SPEED * 1000" | bc -l | awk -F. '{print $1}')
# Check if the speeds are below the minimum threshold
if (( DOWNLOAD_SPEED < MIN_DOWNLOAD_SPEED || UPLOAD_SPEED < MIN_UPLOAD_SPEED )); then
# Send a desktop notification
notify-send "Low Network Speed" "Download speed: ${DOWNLOAD_SPEED}kbps, Upload speed: ${UPLOAD_SPEED}kbps"
fi
Make sure to make the file executable using the chmod command!
Once done, use the following syntax to add lines in the cron table to run the script every 5 minutes:
*/5 * * * * /path/to/script
I have saved this file with the Speed.sh
filename. So I will be adding the following lines in the cron job file to make it run every 5 minutes:
*/5 * * * * /home/sagar/Speed.sh
That's it!
For every 10 minutes (checking disk space)
In this example, I will be using a bash script that checks the disk space and if it's less than 20%, it sends the notification on the desktop.
Here's the script that I will be using:
#!/bin/bash
# Set the minimum free disk space (in percentage)
MIN_FREE_SPACE=20
# Get the percentage of free disk space
FREE_SPACE_PERCENT=$(df -h / | awk '/\// {print $(NF-1)}')
# Remove the percent sign and convert to integer
FREE_SPACE_PERCENT=${FREE_SPACE_PERCENT%\%}
FREE_SPACE_PERCENT=$((FREE_SPACE_PERCENT))
# Check if the free space is below the minimum threshold
if (( FREE_SPACE_PERCENT < MIN_FREE_SPACE )); then
# Send a desktop notification
notify-send "Low Disk Space" "Free space: ${FREE_SPACE_PERCENT}%"
fi
To run the script every 10 minutes, you'd have to use the following command syntax to add the lines in the cron table:
*/10 * * * * /path/to/script
I have saved the script with the Disk.sh
filename and is located in my home directory so I will be using the following lines in my cron table to run this script every 10 minutes:
*/10 * * * * /home/sagar/Disk.sh
For every 15 minutes (check updates using apt)
In this example, I will be using a script that checks the updates using the apt package manager and if there's any update, I will be notified.
Here's the script that I will be using:
#!/bin/bash
# Update the package lists
apt update > /dev/null 2>&1
# Check if any packages can be upgraded
UPGRADE_LIST=$(apt list --upgradable 2>/dev/null)
if [[ ! -z $UPGRADE_LIST ]]; then
# Send a desktop notification
notify-send "Package Update Available" "There is at least one package that can be upgraded."
fi
You can refer to the following command syntax to add the script in the cron table that executes the script every 15 minutes:
*/15 * * * * /path/to/script
The name of my script is Update.sh
and is located in my home directory so I will be using the following lines in my cron table:
*/15 * * * * /home/sagar/Update.sh
That's it!
Learn more on cron jobs
If you would like to learn more about the concept of cron jobs, we have a detailed guide so you can have better clarity:
I hope you will find this guide helpful. And if you have any doubts or suggestions, leave a comment.
A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.