- Show all Docs ( 45 ) Collapse Docs
- Show all Docs ( 457 ) Collapse Docs
- Show all Docs ( 1 ) Collapse Docs
- Show all Docs ( 47 ) Collapse Docs
How to schedule your task using crontab
In this article, we will learn how to schedule your task using crontab. The crontab programme is used to add, remove, or list the tables that tell the cron daemon what to do.
A crontab file tells the cron daemon to do something, like "run this command at this time on this date." Each user can have their own crontab. These files are in the /var directory, but you shouldn't edit them directly.
A cron job is any task that you schedule using crons. Cron jobs help us automate tasks that we do every hour, every day, every month, or every year.
- To allow user to use crontab, make entry in- /etc/cron.allow
- To deny user to use crontab, make entry in- /etc/cron.deny
By default, every user is allowed to use crontab to schedule their tasks. But if we want to deny any particular user, make an entry in above file number 2 using vim or any other way you like.
How to use crontab
Before using crontab, we need to understand the syntax to schedule any task using crontab.
Below is the syntax summary to use crontab
* * * * * Command_to_execute Argument_for_command | | | | | | | | | Day of the Week ( 0 - 6 ) ( Sunday = 0 ) | | | | | | | Month ( 1 - 12 ) | | | | | Day of Month ( 1 - 31 ) | | | Hour ( 0 - 23 ) | Min ( 0 - 59 )
In the above summary, every asterisk( * ) represent a different time period of different values.
|Asterisk name( from left to right)||Value||Description|
|Minute of the hour||0-59||Command would be executed at the specific minute.|
|Hours of the day||0-23||Command would be executed at the specific hour.|
|Day of the Month||1-31||Commands would be executed in these days of the months.|
|Month of the year||1-12||The month in which tasks need to be executed.|
|Weekdays||0-6||Days of the week where commands would run. Here, 0 is Sunday.|
- To add/ delete/ edit a cron job
# crontab -e
- To list the cronjobs of the current user
# crontab -l
- To remove/ deinstall the cronjobs
# crontab -r
- To list another user's cronjobs
# crontab -u username -l
- To edit another user's cronjobs.
# crontab -u username -e
To test the crontab, first create a script called test
.sh which prints the system date and time and appends it to a file.
# vim test.sh
#!/bin/sh date >> testfile
To make sure that this script executes, we need to give it executable permission
# chmod +x test.sh
To run /usr/bin/test.sh at every minute
* * * * * /usr/bin/sh test.sh
To run test.sh everyday at 10pm (22:00)
0 22 * * * /usr/bin/sh test.sh
To restart the httpd service every Saturday at 1am (01:00)
0 1 * * 7 /usr/bin/systemctl restart httpd
To run test.sh at 07:30, 09:30 13:30 and 15:30
30 7,9,13,15 * * * /usr/bin/sh test.sh