A cron job is a Linux command used for scheduling tasks to be executed sometime in the future. This is normally used to schedule a job that is executed periodically. All the Linux Distribution occurs with this advantage and it is very useful when anyone desires to automate any Linux Command or script such as updating systems or packages, generating a backup of data, posting emails, etc. Cronjobs are helpful in repeated tasks like checking the total number of files in a particular directory every 30 minutes or getting the server stats in every 5 minutes to make sure all the services are working perfectly.
Cronjob runs with the help of crontab, that's called cron table. It is a configuration file for all the cron jobs in a particular system. Every user can set any cronjob as per their requirements, so every linux user has their configuration file. A crontab file comprises directions for the cron utility in the following simplified manner: “run this command at this time on this date”. The following image will shows how the cron table configuration file looks like when any cron is added along with time and command. In this image, each line of a crontab file represents a job.
crontab -1 to display the contents of the crontab file of the currently logged-in user.
crontab -e to edit the current user’s cron jobs.
If you are using crontab edit for first the time, you will receive the below message.
no crontab for user – using an empty one
Select an editor. To change later, run ‘select-editor’.
1. /bin/nano <—- easiest
Choose 1-4 :
You can select any editor of your option and can get the cron list menu. In this file, you need to add your cron jobs one by one.
Run the below command to edit the crontab of another user, test:
crontab -u test -e
To set the cronjobs, we are giving some examples below:
>Run a cron job at every minute
* * * * * <command-to-execute>
For example, if the time is 2:00, the next job will run at 2:01, 2:02, 2:03 and so on.
>Run cron job at every 5th minute
*/5 * * * * <command-to-execute>
For example, if the time is 2:00, the next job will run at 2:05, 2:10, 2:15 and so on.
>Run a cron job every hour at minute 30
30 * * * * <command-to-execute>
For example, if the time is 2:00, the next job will run at 2:30, 3:30, 4:30 and so on.
>Run a job every day at 3 am
0 3 * * * <command-to-execute>
>Run a job every Monday
0 0 * * MON <command-to-execute>
It will run at exactly at 00:00 on Monday.
>Run a job every 6 months
0 0 1 */6 * <command-to-execute>
This cron job will start at 00:00 on day-of-month 1 in every 6th month.
We hope this article will be very helpful to list and edit the cronjobs in Linux OS.