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

Huawei cloud developer community 2021-09-15 09:37:38
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

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 .

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/20210909145046984K.html

  1. The first starcoin & move hacksong source code analysis - P (a)
  2. Zhaijia 36 days Salt Fish turn into Tencent, Zookeeper Consistency level analysis,
  3. Traitement de l'interception des champs de demande communs de Spring Cloud, plus de 500 personnes interviewent Ali,
  4. About JavaScript modules
  5. Object oriented programming (2)
  6. Java日期时间API系列42-----一种高效的中文日期格式化和解析方法
  7. Java日期時間API系列42-----一種高效的中文日期格式化和解析方法
  8. 宅家36天鹹魚翻身入職騰訊,Zookeeper一致性級別分析,
  9. Java Date Time API Series 42 - - a efficient Chinese Date Format and Analysis Method
  10. 已成功拿下字节、腾讯、脉脉offer,7年老Java一次操蛋的面试经历,
  11. 小米Java社招面试,每次面试必问的二叉树的设计与编码,
  12. 小米Java校招面试,阿里、百度、美团、携程、蚂蚁面经分享,
  13. 小米Java校招面試,阿裏、百度、美團、攜程、螞蟻面經分享,
  14. Xiaomi Java School Recruitment interview, Ali, baidu, meituan, ctrip, ant Facebook Sharing,
  15. La conception et le codage de l'arbre binaire requis pour chaque entrevue d'embauche de la société Java millet;
  16. A remporté avec succès Byte, Tencent, Pulse offer, 7 ans Java une expérience d'entrevue de baise,
  17. 干货来袭,Java岗面试12家大厂成功跳槽,
  18. 常用Java框架面试题目,现在做Java开发有前途吗?
  19. 常用Java框架面試題目,現在做Java開發有前途嗎?
  20. Les questions d'entrevue couramment utilisées pour le cadre Java sont - elles prometteuses pour le développement Java?
  21. L'arrivée de marchandises sèches, l'entretien d'emploi Java 12 grandes usines ont réussi à changer d'emploi,
  22. Multiple postures for handling container time in k8s environment
  23. Echarts remove left Gap, Blank
  24. Hotspot Weekly | zoom $100 million, docker fees, $38 billion Data bricks
  25. JsonMappingException: No serializer found for class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory...
  26. Java. Security. Securerandom source code analysis Java. Security. EGD = file: / dev /. / urandom
  27. When using IntelliJ idea, jump directly and quickly from the mapper interface to mapper.xml
  28. When idea writes SQL in mybatis XML, the solution to the problems of table name, field and red reporting
  29. Spring cloud integrates Nacos
  30. 应届毕业生Java笔试题目,2021大厂Java社招最全面试题,
  31. Liver explosion! Take you to understand Hadoop serialization
  32. linux系列之:告诉他,他根本不懂kill
  33. java版gRPC实战之三:服务端流
  34. RabbitMQ核心知识总结!
  35. linux系列之:告诉他,他根本不懂kill
  36. java版gRPC实战之三:服务端流
  37. RabbitMQ核心知识总结!
  38. 10天拿到字节跳动Java岗位offer,学习Java开发的步骤
  39. 10天拿到字节跳动Java岗位offer,Java知识点思维导图
  40. Résumé des connaissances de base de rabbitmq!
  41. 10天拿到字節跳動Java崗比特offer,Java知識點思維導圖
  42. 10 jours pour obtenir un Byte Jump Java post offer, Java Knowledge point Mind Map
  43. 10 jours pour obtenir l'octet Jump Java post offer, apprendre les étapes du développement Java
  44. Java version of gppc Reality Three: server side stream
  45. Linux Series: Dites - lui qu'il ne connaît pas kill du tout
  46. "Data structure and algorithm" of front end -- binary search
  47. 2020-2021京东Java面试真题解析,如何才能通过一线互联网公司面试
  48. 13 SpringBoot整合RocketMQ实现过滤消息-根据SQL表达式过滤消息
  49. 12 SpringBoot整合RocketMQ实现过滤消息-根据TAG方式过滤消息
  50. 11 SpringBoot整合RocketMQ实现事务消息
  51. 11 springboot Consolidated rocketmq Implementation transaction message
  52. 12 springboot Consolidated rocketmq Implements Filtering messages - Filtering messages according to tag method
  53. 13 springboot Consolidated rocketmq Implementation Filtering messages - Filtering messages according to SQL expressions
  54. linux系列之:告诉他,他根本不懂kill
  55. (1)java Spring Cloud+Spring boot企业快速开发架构之微服务是什么?它的优缺点有哪些?
  56. Oracle 检查 DATE 列 RANGE 分区表已有分区的最大日期时间
  57. ConcurrentHashMap--原理
  58. 2020 - 2021 JD Java interview Real question Analysis, How can interview through First - Line Internet Company
  59. Concurrenthashmap - - Principes
  60. Oracle vérifie l'heure de date maximale d'une partition existante dans la colonne date