Category: Linux Terminal

Added: 20th of October 2020

Viewed: 632 times

Create a simple cronjob in Linux

In Linux you can automate tasks by creating a cronjobs. This can be especially helpful if for example you wanted to created a backup of your database everyday, calling a bash or .php script.

Getting your head around Cronjobs can be confusing at the start, so I will attempt to explain how they work with some really simple examples.

There are two parts to a cronjob. The first part is telling the system when you want to run the cron job, and the second part is telling the system what you want it to do using linux commands.

Part 1: Telling the system when to run your cronjob

This is set out using the following parameters
m=Minute
h=Hour
dom=Day of Month
mon=Month
dow=Day of Week

Run a cronjob at 10am on the 25th of December

m
h
dom
mon
dow

0
10
25
12
*


Run a cronjob at 10:10am on the 25th of December

m
h
dom
mon
dow

10
10
25
12
*


Run a cronjob at 10:10am on the 25 of Every Month

m
h
dom
mon
dow

10
10
25
*
*


Run a cronjob at 10:10am every day of the year

m
h
dom
mon
dow

10
10
*
*
*


Run a cronjob every minute, of every day, all year

m
h
dom
mon
dow

*
*
*
*
*



Part 2: Telling the system what to do
In this part you set your command. To keep things really simple we copy an image from the Pictures folder to the Desktop.
cp /home/username/Pictures/tux.png /home/username/Desktop


When we put it all together we get the following cron statement.
At 10:00am on the 25th December copy tux.png from the Pictures folder to Desktop
0 10 25 12 * cp /home/username/Pictures/tux.png /home/username/Desktop


How to set the cron job
To create a working cron job open your terminal and enter the following command.
crontab -e


Please note: You might want to set the time and date nearer to your own times and dates so you can see the cronjob in action.

You will notice the terminal will open with nano editor. Enter the information below the commented out section. Remember to replace username with your own username

0 10 25 12 * cp /home/username/Pictures/tux.png /home/username/Desktop






Once you have entered the information, do the following

1) Press CTRL + o to save, and then press enter to confirm.
2) Press CTRL + x to exit nano

Your cronjob has now been saved.

To view the cronjob you just created, open your terminal and enter the following command
crontab -l




If you have set the times and date nearer to your own time and date, the cronjob should now be ready to go.

This is a just a very basic example and only covers a small part of cron, but is enough to give you a starting point.

If you visit https://crontab.guru/ you can try different cron schedule expressions

Other Tips & Tutorials

Linux Terminal

Kill multiple processes using the pkill command in Linux

Posted: 5th of January 2021

Views: 400

Linux Terminal

Display free memory using the terminal in Linux

Posted: 17th of December 2020

Views: 499

Linux Terminal

Using mlocate to find files by name, extension using the terminal in Linux

Posted: 27th of November 2020

Views: 422

Linux Terminal

Capture screenshots of websites using cutycapt in different formats using the terminal in Ubuntu and Ubuntu based distributions

Posted: 15th of November 2020

Views: 556

Linux Terminal

Display a calendar in the terminal

Posted: 6th of November 2020

Views: 505

Linux Terminal

Create a simple cronjob in Linux

Posted: 20th of October 2020

Views: 632

Linux Terminal

Create a new folder and file using the mkdir and touch commands using the terminal in Linux

Posted: 16th of August 2020

Views: 576

Linux Terminal

Convert a PDF document to text or html using pdftotext, pdftohtml in Linux using the terminal

Posted: 3rd of August 2020

Views: 877

Linux Terminal

List files in a directory using the ls command

Posted: 22nd of July 2020

Views: 611

Linux Terminal

How to access HP Linux Imaging and Printing System set up on Ubuntu using the terminal

Posted: 13th of November 2019

Views: 1307