15 useful cron work examples commonly used by Senior Linux system administrators

Huawei cloud developer community 2021-09-15 10:08:36
Abstract :Linux Cron A utility is a program that works at a specific time and / Or date is an effective method to continuously schedule routine background jobs . This article tells about 15 A very useful one crontab Work scheduling example .

This article is shared from Huawei cloud community 《Linux Crontab:15 A very useful one Cron Examples of work 》, author :Tiamo_T.

Experienced Linux The system administrator knows the importance of automatically running daily maintenance jobs in the background .

Linux Cron A utility is a program that works at a specific time and / Or date is an effective method to continuously schedule routine background jobs .

This article tells about 15 A very useful one crontab Work scheduling example .

Linux Crontab Format

MIN HOUR DOM MON DOW CMD
image.png

1. Schedule work for a specific time

cron The basic usage of is to execute a job at a specific time , As shown below . This will be 6 month 10 The morning of 08:30 Perform a full backup shell Script ( Full backup ).

Please note that , The time field uses 24 Hour format . therefore , For the morning 8 Point use 8, For the night 8 Point use 20.

30 08 10 06 * /home/ramesh/full-backup
• 30 – 30 minute
• 08 – 08 In the morning
• 10 – The first 10 God
• 06 – The first 6 Months ( June )
• * – Every day of the week

2. Scheduling jobs for multiple instances ( for example , Twice a day )

The following script performs incremental backups twice a day .

This example is displayed on a daily basis 11:00 and 16:00 Perform the specified incremental backup shell Script (incremental-backup). The comma separated value in the field specifies that the command needs to be executed at all times mentioned .

00 11,16 * * * /home/ramesh/bin/incremental-backup

• 00 – The first 0 minute ( The top of an hour )
• In the morning 11,16 – 11 And in the afternoon 4 spot
• * – Every day
• * – Every month
• * – Every day of the week

3. Schedule work for a specific time frame ( For example, only on weekdays )

If you want to schedule an assignment every hour within a specific time frame , Please use the following .

The number of working hours per day Cron Job

This example is used every morning 9 Point to afternoon 6 During working hours at ( Including weekends ) Check the status of the database

00 09-18 * * * /home/ramesh/bin/check-db-status

• 00 – The first 0 minute ( The top of an 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 Working hours per working day

This example occurs every working day ( That is, excluding Saturdays and Sundays ) Our working hours are in the morning 9 Point to afternoon 6 Check the status of the database

00 09-18 * * 1-5 /home/ramesh/bin/check-db-status

• 00 – The first 0 minute ( The top of an 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 entry ?

View the of the currently logged in user Crontab entry

To view your crontab entry , Please start from your unix Account type crontab -l, 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 entry

With root user (su – root) Log in and execute crontab -l , As shown below .

root@dev-db# crontab -l
no crontab for root

Crontab HowTo: See more Linux User Crontab entry

To view other Linux User crontab entry , Please 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 entry ?

Edit the name of the currently logged in user Crontab entry

To edit crontab entry , Please use crontab -e, As shown below . By default , This will edit the currently logged in user crontab.

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 When saving the above temporary files , It will preserve crontab And display the following message , indicate crontab Modification successful .

~
"crontab.XXXXyjWkHw" 2L, 83C written
crontab: installing new crontab

Edit root Crontab entry

With root user (su – root) Log in and execute crontab -e, As shown below .

root@dev-db# crontab -e
Edit other Linux User Crontab File entry

To edit other Linux User crontab entry , Please 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 One assignment per minute .

Ideally , You may not need to schedule your 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 throughout the year . In addition to using this directly *, You will find it very useful in the following situations .

• When you specify... In the minutes field */5 when , each 5 Minutes at a time .
• When you specify... In the minutes field 0-10/2 when , It means before 10 Every... In minutes 2 Minutes at a time .
• therefore , The above agreement can be used for all other purposes 4 A field .

7. Every time 10 Arrange a backstage in minutes Cron Homework .

If you want every 10 Check disk space every minutes , Please use the following methods .

*/10 * * * * /home/ramesh/check-disk-space

It every year 10 Execute the specified command once per minute check-disk-space. However, you may need to execute commands only during office hours , vice versa . The above example shows how to do these things .

We can use a single keyword to specify it , Not in 5 Specify a value in a field , As follows .

In some special cases , You can use @ Followed by keywords to replace the above 5 A field , For example, restart 、 The middle of the night 、 Every year, 、 Every hour .
image.png

8. Use @yearly Arrange work at the first minute of each year

If you want to do a job at the first minute of each year , Then you can use @yearly cron keyword , As shown below .

This will happen every year 1 month 1 Japanese 00:00 Use annual maintenance shell Script execution system annual maintenance .

@yearly /home/ramesh/red-hat/bin/annual-maintenance

9. Use @monthly Schedule the start of each month Cron Homework

It's with the @yearly be similar . But use @monthly cron Keyword execute the command once a month .

This will happen every month 1 Japanese 00:00 perform shell Script tape backup .

@monthly /home/ramesh/suse/bin/tape-backup

10. Daily use @daily Schedule background jobs

Use @daily cron keyword , This will happen every day 00:00 Use cleanup-logs shell The script executes daily log file cleanup .

@daily /home/ramesh/arch-linux/bin/cleanup-logs "day started"

11. How to use... After each restart @reboot perform Linux command ?

Use @reboot cron keyword , This will execute the specified command once every time the machine starts .

@reboot CMD

12. How to use MAIL Keyword disable / Redirect Crontab Mail output ?

By default ,crontab Send the job output to the user who scheduled the job . If you want to redirect the output to a specific user , Please be there. crontab Add or update MAIL Variable , 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 , Stop sending via email crontab Output , Please be there. crontab Add or update MAIL Variable , As shown below .

MAIL=""

13. How to use Crontab Once a second Linux Cron Homework .

You can't schedule... Once a second cronjob. Because in cron in , The minimum 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. stay Crontab It is specified in PATH Variable

In all the above examples, we have specified the Linux Order or shell-script The absolute path of .

for example , If you just want to specify tape-backup, Instead of specifying /home/ramesh/tape-backup, The path /home/ramesh Add to crontab Medium PATH variable , 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. from Cron Files installed Crontab

In addition to direct editing crontab Outside the document , You can also add all entries to cron In file . After all these entries are included 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

Be careful : This will cron-file.txt Install to your crontab, This will also delete your old cron entry . therefore , from cron-file.txt Upload cron Please note when entering .

Click to follow , The first time to learn about Huawei's new cloud technology ~

Please bring the original link to reprint ,thank
Similar articles

2021-09-15

2021-09-15

2021-09-15

2021-09-15

2021-09-15