Abstract: The Linux Cron utility is an effective way to continuously schedule routine background jobs at a specific time and/or date. This article describes 15 useful crontab job scheduling examples.
This article is shared from Huawei Cloud Community " Linux Crontab: 15 useful Cron working examples ", author: Tiamo_T.
Experienced Linux system administrators know the importance of running daily maintenance tasks automatically in the background.
The Linux Cron utility is an effective way to continuously schedule routine background jobs at a specific time and/or date.
This article describes 15 useful crontab job scheduling examples.
Linux Crontab format
MIN HOUR DOM MON DOW CMD
1. Schedule homework for a specific time
The basic usage of cron is to execute a job at a specific time, as shown below. This will June 10 morning 08:30 perform a full backup shell script (full backup).
Note that the time field uses a 24-hour format. Therefore, use 8 for 8 AM and 20 for 8 PM.
30 08 10 06 * /home/ramesh/full-backup
• 30 – 30 minutes
• 08 – 08 am
• 10 – Day 10
• 06 – 6th month (June)
• * – every day of the week
2. Schedule jobs for multiple instances (for example, twice a day)
The following script performs incremental backups twice a day.
This example executes the specified incremental backup shell script (incremental-backup) at 11:00 and 16:00 every day. The comma-separated value in the field specifies that the command needs to be executed in all the time mentioned.
00 11,16 * * * /home/ramesh/bin/incremental-backup
• 00 – Minute 0 (the top of the hour)
• 11, 16 – 11 am and 4 pm
• * - every day
• * – every month
• * – every day of the week
3. Schedule work for a specific time frame (for example, only on working days)
If you want to schedule an assignment every hour within a certain time frame, please use the following.
Cron Job for daily working hours
This example checks the status of the database during working hours (including weekends) from 9 am to 6 pm every day
00 09-18 * * * /home/ramesh/bin/check-db-status
• 00 – Minute 0 (the top of the hour)
• 09-18 – 9 am, 10 am, 11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
• * - every day
• * – every month
• * – every day of the week
Cron Job's working hours per working day
This example checks the status of the database from 9 am to 6 pm during working hours on every working day (that is, excluding Saturdays and Sundays)
00 09-18 * * 1-5 /home/ramesh/bin/check-db-status
• 00 – Minute 0 (the top of the hour)
• 09-18 – 9 am, 10 am, 11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
• * - every day
• * – every month
• 1-5-Monday, Tuesday, Wednesday, Thursday and Friday (every working day)
4. How to view Crontab entries?
View the Crontab entry of the currently logged in user
To view your crontab entries, type crontab -l from your unix account as shown below.
ramesh@dev-db$ crontab -l
@yearly /home/ramesh/annual-maintenance
*/10 * * * * /home/ramesh/check-disk-space
[Note: This displays crontab of the current logged in user]
View root Crontab entries
Log in as the root user (su-root) and execute crontab -l as shown below.
root@dev-db# crontab -l
no crontab for root
Crontab HowTo: View Crontab entries of other Linux users
To view the crontab entries of other Linux users, log in to root and use -u {username} -l as shown below.
root@dev-db# crontab -u sathiya -l
@monthly /home/sathiya/monthly-backup
00 09-18 * * * /home/sathiya/check-db-status
5. How to edit Crontab entries?
Edit the Crontab entry of the currently logged-in user
To edit crontab entries, use crontab -e as shown below. By default, this will edit the crontab of the currently logged in user.
ramesh@dev-db$ crontab -e
@yearly /home/ramesh/centos/bin/annual-maintenance
*/10 * * * * /home/ramesh/debian/bin/check-disk-space
~
"/tmp/crontab.XXXXyjWkHw" 2L, 83C
[Note: This will open the crontab file in Vim editor for editing.
Please note cron created a temporary /tmp/crontab.XX... ]
When you use :wq to save the above temporary file, it will save crontab and display the following message, indicating that the crontab has been modified successfully.
~
"crontab.XXXXyjWkHw" 2L, 83C written
crontab: installing new crontab
Edit the root Crontab entry
Log in as the root user (su-root) and execute crontab -e as shown below.
root@dev-db# crontab -e
Edit Crontab file entries for other Linux users
To edit the crontab entries of other Linux users, log in to root and use -u {username} -e as shown below.
root@dev-db# crontab -u sathiya -e
@monthly /home/sathiya/fedora/bin/monthly-backup
00 09-18 * * * /home/sathiya/ubuntu/bin/check-db-status
~
~
~
"/tmp/crontab.XXXXyjWkHw" 2L, 83C
6. Use Cron to schedule a job every minute.
Ideally, you might not need to schedule homework every minute. But understanding this example will help you understand the other examples mentioned below in this article.
* * * * * CMD
- Represents all possible units—that is, every minute of every hour of the year. In addition to using this* directly, you will find it very useful in the following situations.
• When you specify */5 in the minutes field, it means once every 5 minutes.
• When you specify 0-10/2 in the minutes field, it means every 2 minutes for the first 10 minutes.
• Therefore, the above convention can be used for all other 4 fields.
7. Schedule a background cron job every 10 minutes.
If you want to check the disk space every 10 minutes, please use the following method.
*/10 * * * * /home/ramesh/check-disk-space
It executes the specified command check-disk-space every 10 minutes throughout the year. But you may need to execute orders only during office hours, and vice versa. The example above shows how to do these things.
We can use a single keyword to specify it instead of specifying values in 5 fields, as described below.
In some special cases, you can use @ followed by keywords to replace the above five fields, such as restart, midnight, every year, and every hour.
8. Use @yearly to schedule work in the first minute of each year
If you want to perform a job in the first minute of each year, then you can use the @yearly cron keyword as shown below.
This will use the annual maintenance shell script to perform annual system maintenance at 00:00 on January 1st each year.
@yearly /home/ramesh/red-hat/bin/annual-maintenance
9. Use @monthly to schedule cron jobs starting every month
It is similar to @yearly above. But use the @monthly cron keyword to execute the command once a month.
This will perform a shell script tape backup at 00:00 on the 1st of every month.
@monthly /home/ramesh/suse/bin/tape-backup
10. Use @daily to schedule background jobs every day
Using the @daily cron keyword, this will perform daily log file cleanup at 00:00 every day using the cleanup-logs shell script.
@daily /home/ramesh/arch-linux/bin/cleanup-logs "day started"
11. How to use @reboot to execute Linux commands after each restart?
Use the @reboot cron keyword, which will execute the specified command every time the machine is started.
@reboot CMD
12. How to use the MAIL keyword to disable/redirect Crontab mail output?
By default, crontab sends job output to the user who scheduled the job. If you want to redirect the output to a specific user, add or update the MAIL variable in crontab as shown below.
ramesh@dev-db$ crontab -l
MAIL="ramesh"
@yearly /home/ramesh/annual-maintenance
*/10 * * * * /home/ramesh/check-disk-space
[Note: Crontab of the current logged in user with MAIL variable]
If you don't want to send mail anywhere, that is, stop sending crontab output via email, please add or update the MAIL variable in crontab as shown below.
MAIL=""
13. How to use Crontab to execute Linux Cron jobs every second.
You cannot schedule a cronjob every second. Because in cron, the smallest unit you can specify is minutes. In a typical scenario, most of us have no reason to run any jobs in the system every second.
14. Specify the PATH variable in Crontab
In all the above examples, we have specified the absolute path of the Linux command or shell-script that needs to be executed.
For example, if you only want to specify tape-backup instead of specifying /home/ramesh/tape-backup, add the path /home/ramesh to the PATH variable in crontab as shown below.
ramesh@dev-db$ crontab -l
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/home/ramesh
@yearly annual-maintenance
*/10 * * * * check-disk-space
[Note: Crontab of the current logged in user with PATH variable]
15. Install Crontab from Cron file
In addition to editing the crontab file directly, you can also add all entries to the cron file first. After including all these entries in the file, you can upload or install them to cron as shown below.
ramesh@dev-db$ crontab -l
no crontab for ramesh
$ cat cron-file.txt
@yearly /home/ramesh/annual-maintenance
*/10 * * * * /home/ramesh/check-disk-space
ramesh@dev-db$ crontab cron-file.txt
ramesh@dev-db$ crontab -l
@yearly /home/ramesh/annual-maintenance
*/10 * * * * /home/ramesh/check-disk-space
Note: This will install cron-file.txt to your crontab, which will also delete your old cron entries. Therefore, please pay attention when uploading cron entries from cron-file.txt.
Click to follow and learn about Huawei Cloud's fresh technology for the first time~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。