How to schedule cron jobs with crontab in Linux

Ravindra Kumar
3 min readOct 2, 2020

The process of creating the jobs and make them occur on the system repeatedly hourly, daily, weekly, monthly and yearly is called Job scheduling. Crontab is an important command-line utility that is used for scheduling jobs that occur at the specified time. The Scheduled job can be a single command or we can also schedule cron to run bash shell scripts.The crontab helps us to automate repetitive tasks like backups, system updation, running some specific command, bash shell scripts, etc. Let’s understand how to schedule cron Jobs with crontab in Linux.

Importance of Job Scheduling

The importance of the job scheduling is that the critical tasks like backups, which the client usually wants to be taken in nights, can easily performed without the intervention of the administrator by scheduling a cron job. You can schedule cron jobs and system will execute it without human intervention. Cron jobs can be scheduled to run on every minute, hour, day of month, month, day of week basis. You can also combine these to schedule cron jobs.

The standard syntax of crontab is given below.

<minutes> <hours> <day of the month> <month of the year> <day of the week> <job or script>
(0–59) (0–23) (1–31) (1–12 or jan, feb,..) (0–6 or sun, mon,..)

*

treated as a wild card. Meaning any possible value.

*/10

treated as every 10 minutes, hours, days or months. Replacing he 5 with any numerical value will change this option.

2,4,6

treated as an OR, so if placed in the hours, this could mean at 2, 4 or 6 o-clock

9–17

treats for any value between 9 and 17. So if placed in day of the month this would be days 9 through 17 or if put in hours, it would be between 9 AM and 5 PM.

There is crontab utility to schedule a job or task.

$ sudo crontab -e$ sudo crontab -l$ sudo crontab -r

Note: Instead of removing cron job, it is recommended to comment out cronjobs by putting # (hash symbol) before cron entries.

Here, you can replace ‘user_name’ with the desired username. By default, the root user can access to cron jobs for all users .

Few Examples of Scheduling cron jobs

Let’s have a look into few examples of scheduled cronjobs. Edit the crontab using “crontab -e” command in shell.

58 14,15 20-25 1 2,3,6 /usr/sbin/passwd

where.
58 → 58 minutes
14,15 → 14 hours and 15 hours i.e 2PM and 3PM ( 14:58 and 15:58)
20–25 → dates 20, 21, 22. 23, 24 and 25
1 → January
2, 3, 6 → 2nd day, 3rd day and 6th day

where,
58 → 58 minutes
15 → 15 hours (at 15:58)
* * * → every day

00 23 * * * /home/user_name/backup.sh30 23 * * 5 /home/user_name/weekly_backup.sh55 14 20 1 2 /usr/sbin/useradd Ravi; usr/sbin/groupadd Team

Hence, you learnt How to Schedule Cron Jobs with Crontab in Linux. Stay tuned for next linux tutorials.

Read Also : How to Configure LAMP Stack on CentOS 7

Originally published at https://thecodecloud.in on October 2, 2020.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Ravindra Kumar
Ravindra Kumar

Written by Ravindra Kumar

Hi, This is Ravi. I am Senior DevOps Engineer.. I love to write technical blogs.

No responses yet

Write a response