Cronjob is one of the basic and most useful utility that you can find on almost all the Linux distributions. A cronjob is basically a task scheduled to run at a specific time. For example you can setup a cronjob that will run everyday to take backup of your files. Following are the details on how to setup a cronjob, its basic commands and syntax.
Every Cronjob entry has to satisfy the following syntax:
Minute (0-59) Hour(0-23) Day_of_Month (1-31) Month(1-12) Day_of_week (0-6 SUN-SAT) Command_to_execute
You can use following commands to when working with Cronjobs:
To list all the Cronjobs that are already setup.
$ conjob -l
To edit / add / remove the Crontabs
$ crontab -e
if you are sudo user you can use -u option to list the Cronjobs of other users same is true for -e option.
$ sudo crontab -l -u user_name
$ sudo crontab -e -u user_name
If Cronjobs are being setup first time on the system it will ask for you to choose an editor which will be used for Cronjobs editing.
Once you have opened the Cronjobs in an editor it will look as follows.
Here in the editor you can add modify or delete the Cronjobs.
Lets have a look at different examples of Cronjobs
Cronjob to run every minute:
* * * command_to_execute
Every 15 minute Cronjob
*/15 * * * * command_to_execute
you can also give comma separated values, to run cronjob every 0, 5, 10 minute of every hour add:
0,5,10 * * * * command_to_execute
To run Cronjob every hour add:
0 * * * * command_to_execute
you can also run Cronjob every 4 hour:
0 */4 * * * command_to_execute
To run Cronjob everyday, following will run everyday at 00:00
0 0 * * * command_to_execute
Run a Cronjob every Sunday:
0 0 * * SUN command_to_execute OR 0 0 * * 0 command_to_execute
Run a Cronjob every Month:
0 0 1 * * command_to_execute
Run a Job every 6 months:
0 0 1 */6 * command_to_execute
Other than defining the timing you can also use predefined strings to setup crojnobs:
To run a Cronjob every time system reboot
Following will run cronjob every year
Run a Cronjob Monthly
Above will give you basic understanding of how to work with Cronjobs. Did it help you let us know in comments section.