What is Crontab?
If you want to perform regular operations(for example schedule a task) on Linux, you should create a cron job.
Create a Crontab Job
To create a Crontab Job, we first connect to the server as root.
Then run the below command and press the insert key from the keyboard.
1 |
crontab -e |
Finally, we create the job by pasting the following line.
1 |
00 09 * * * sh /xx/postgres_scripts/diskusage.sh |
On the above line, the sh command after “00 09 * * *” indicates that we will run an sh file.
The command below specifies that the diskusage.sh file in the “xx / postgres_scripts” directory will work.
1 |
/xx/postgres_scripts/diskusage.sh |
let’s enumerate the parameters in the above line and look at meaning of each number.
1 |
1 2 3 4 5 sh /xx/postgres_scripts/diskusage.sh |
1 | It specifies the minute the job will work(0-59) |
2 | It specifies the hour the job will work(0-23) |
3 | It specifies the day the job will work(1-31) |
4 | It specifies the month the job will work(1-12) |
5 | It specifies what day of the week the job will work.(0-7) Sunday=0 or 7 |
Let’s make some examples.
Example1:
1 |
30 16 7 12 * sh /xx/postgres_scripts/diskusage.sh |
Crontab job to run every minutes
1 |
* * * * * sh /xx/postgres_scripts/diskusage.sh |
Cron job to run every 5 minutes
1 |
*/5 * * * * sh /xx/postgres_scripts/diskusage.sh |
Cron job to run every 10 minutes
1 |
*/10 * * * * sh /xx/postgres_scripts/diskusage.sh |
Cron job to run every 15 minutes
1 |
*/15 * * * * sh /xx/postgres_scripts/diskusage.sh |
Cron job to run every 30 minutes
1 |
*/30 * * * * sh /xx/postgres_scripts/diskusage.sh |
Cron job to run every hour
1 |
0 * * * * sh /xx/postgres_scripts/diskusage.sh |
Cron job to run every 2 hours
1 |
* */2 * * * sh /xx/postgres_scripts/diskusage.sh |
Cron job to run every 3 hours
1 |
* */3 * * * sh /xx/postgres_scripts/diskusage.sh |
Cron job to run every day
1 |
30 16 * * * sh /xx/postgres_scripts/diskusage.sh |
Cron job to run every week on Sunday
1 |
0 0 * * 0 sh /xx/postgres_scripts/diskusage.sh |
Cron job to run on weekdays
1 |
0 0 * * 1-5 sh /xx/postgres_scripts/diskusage.sh |
Cron job to run on weekend
1 |
0 0 * * 6,0 sh /xx/postgres_scripts/diskusage.sh |
Cron job to run every month
1 |
0 0 1 * * sh /xx/postgres_scripts/diskusage.sh |
Cron job to run every year
1 |
0 0 1 1 * sh /xx/postgres_scripts/diskusage.sh |
Cron job run on specific day
1 |
30 16 * * 6 sh /xx/postgres_scripts/diskusage.sh |
Cron job to run on last day of month
1 |
59 23 28-31 * * [ "$(date +%d -d tomorrow)" = "01" ] && /root/script.sh sh /xx/postgres_scripts/diskusage.sh |
Cron job to run first day of month
1 |
0 0 1 * *&& /root/script.sh sh /xx/postgres_scripts/diskusage.sh |