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

Huawei cloud developer community 2021-09-15 10:08:36
useful cron work examples commonly


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 ~

版权声明
本文为[Huawei cloud developer community]所创,转载请带上原文链接,感谢
https://javamana.com/2021/09/20210909150222126v.html

  1. 10天拿到字節跳動Java崗比特offer,Java知識點思維導圖
  2. 10 jours pour obtenir un Byte Jump Java post offer, Java Knowledge point Mind Map
  3. 10 jours pour obtenir l'octet Jump Java post offer, apprendre les étapes du développement Java
  4. Java version of gppc Reality Three: server side stream
  5. Linux Series: Dites - lui qu'il ne connaît pas kill du tout
  6. "Data structure and algorithm" of front end -- binary search
  7. 2020-2021京东Java面试真题解析,如何才能通过一线互联网公司面试
  8. 13 SpringBoot整合RocketMQ实现过滤消息-根据SQL表达式过滤消息
  9. 12 SpringBoot整合RocketMQ实现过滤消息-根据TAG方式过滤消息
  10. 11 SpringBoot整合RocketMQ实现事务消息
  11. 11 springboot Consolidated rocketmq Implementation transaction message
  12. 12 springboot Consolidated rocketmq Implements Filtering messages - Filtering messages according to tag method
  13. 13 springboot Consolidated rocketmq Implementation Filtering messages - Filtering messages according to SQL expressions
  14. linux系列之:告诉他,他根本不懂kill
  15. (1)java Spring Cloud+Spring boot企业快速开发架构之微服务是什么?它的优缺点有哪些?
  16. Oracle 检查 DATE 列 RANGE 分区表已有分区的最大日期时间
  17. ConcurrentHashMap--原理
  18. 2020 - 2021 JD Java interview Real question Analysis, How can interview through First - Line Internet Company
  19. Concurrenthashmap - - Principes
  20. Oracle vérifie l'heure de date maximale d'une partition existante dans la colonne date
  21. Docker Compose 实践及梳理
  22. Qu'est - ce qu'un microservice pour Java Spring Cloud + Spring Boot Enterprise Quick Development architecture?Quels sont ses avantages et ses inconvénients?
  23. Plus sign interview knowledge points in Java
  24. Pratique et organisation de la composition des dockers
  25. Linux Series: Dites - lui qu'il ne connaît pas kill du tout
  26. Convenient CSS and jQuery drop-down menu solution
  27. Linux analog packet loss rate
  28. Redis:我是如何与客户端进行通信的
  29. 15 useful cron work examples commonly used by Senior Linux system administrators
  30. 24个 JavaScript 循环遍历方法,你都知道吗?
  31. Reading notes of JavaScript advanced programming (3rd Edition) 4
  32. 30分钟学会Docker里面开启k8s(Kubernetes)登录仪表盘(图文讲解)
  33. 24 méthodes de traversée de boucle Javascript, vous savez?
  34. 30 minutes pour apprendre à ouvrir le tableau de bord k8s (kubernets) dans le docker (explication graphique)
  35. Redis: comment je communique avec les clients
  36. Wsl2: Windows native Linux subsystem
  37. 30分钟学会Docker里面开启k8s(Kubernetes)登录仪表盘(图文讲解)
  38. Docker Compose 实践及梳理
  39. Python高级用法总结(8)-函数式编程
  40. 261页前端面试题宝典,JavaScript变量声明提升
  41. The performance of JVM and Java applications of the same version differs by 30% on X86 and aarch64 platforms. Why?
  42. Page 261: dictionnaire des questions d'entrevue de première ligne, promotion de la Déclaration des variables JavaScript
  43. Python Advanced use Summary (8) - functional Programming
  44. Pratique et organisation de la composition des dockers
  45. 30 minutes pour apprendre à ouvrir le tableau de bord k8s (kubernets) dans le docker (explication graphique)
  46. [design pattern series] simple factory
  47. 2021Java最新大厂面试真题,阿里技术专家深入讲解
  48. Java equals and==
  49. Spring5(五)——AOP
  50. 海量列式非关系数据库HBase 原理深入
  51. java collections
  52. 海量列式非關系數據庫HBase 原理深入
  53. Base de données non relationnelle à grande échelle
  54. Printemps 5 (v) - AOP
  55. 2021 le dernier vrai problème d'entrevue d'usine de Java, expliqué en profondeur par les experts techniques d'Ali
  56. Java knowledge structure diagram and thinkinjava PDF file
  57. Do redis transactions satisfy atomicity?
  58. Java programmers must master 10 open source tools!
  59. [Cao Gong's essay] talk about the contract between Maven framework and plug-ins
  60. What books are better for learning Java