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.
Crontab Basic Syntax
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.|
Scheduling a Cron job
There is crontab utility to schedule a job or task.
- Edit a job
$ sudo crontab –e
- Display all cronjobs
$ sudo crontab –l
- Removing a cronjob
$ sudo crontab –r
Note: Instead of removing cron job, it is recommended to comment out cronjobs by putting # (hash symbol) before cron entries.
- Using crontab for other users
$ crontab -u user_name -e
$ crontab -u user_name -l
$ crontab -u user_name -r
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
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
- Running a command everyday at 3:58 PM.
58 15 * * * <command>
58 —–> 58 minutes
15 —–> 15 hours (at 15:58)
* * * —–> every day
- Running a command on every 2 hour.
* */2 * * * <commands>
- Running a backup script named backup.sh located at /home/user_name every night at 11:00 PM,
00 23 * * * /home/user_name/backup.sh
- Running a weekly backup script named weekly_backup.sh on every Friday at 11:30 PM.
30 23 * * 5 /home/user_name/weekly_backup.sh
- This cron job executes the useradd and groupadd commands on Tuesday 20th Jan every year 2:55PM.
55 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