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

Huawei cloud developer community 2021-09-15 06:14:41
useful cron work examples commonly


​​​​​​ Abstract :LinuxCron 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.

 Senior Linux System administrators often use 15 A very useful one Cron Examples of work _ Applications

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
  • 1.
  • 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
  • 1.
  • 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
  • 1.
  • 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
  • 1.
  • 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 .

[email protected]$ crontab -l
@yearly /home/ramesh/annual-maintenance
*/10 * * * * /home/ramesh/check-disk-space
[Note: This displays crontab of the current logged in user]
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

View root Crontab entry

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

[email protected]# crontab -l
no crontab for root
  • 1.
  • 2.

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 .

[email protected]# crontab -u sathiya -l
@monthly /home/sathiya/monthly-backup
00 09-18 * * * /home/sathiya/check-db-status
  • 1.
  • 2.
  • 3.

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.

[email protected]$ 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... ]
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

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
  • 1.
  • 2.
  • 3.

Edit root Crontab entry

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

[email protected]# crontab -e
  • 1.

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 .

[email protected]# 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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

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
  • 1.

* 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
  • 1.

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 .

 Senior Linux System administrators often use 15 A very useful one Cron Examples of work _ Applications _02

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
  • 1.

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
  • 1.

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"
  • 1.

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
  • 1.

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 .

[email protected]$ 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]
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

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=""
  • 1.

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 .

[email protected]$ 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]
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

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 .

[email protected]$ crontab -l
no crontab for ramesh
$ cat cron-file.txt
@yearly /home/ramesh/annual-maintenance
*/10 * * * * /home/ramesh/check-disk-space
[email protected]$ crontab cron-file.txt
[email protected]$ crontab -l
@yearly /home/ramesh/annual-maintenance
*/10 * * * * /home/ramesh/check-disk-space
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

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/20210909121651730n.html

  1. Vous apprendrez à construire un serveur d'applet Wechat (https) à la main
  2. 作为一名程序员我不忘初心,Java最新实习面试经验总结,
  3. 作为一名Java面试者你应该知道的,2021最新Java常用开源库总结,
  4. 作为一个程序员,你觉得最大的悲哀是什么,2021年大厂Java岗面试必问,
  5. Configuration de l'environnement Java du système win10
  6. 作為一個程序員,你覺得最大的悲哀是什麼,2021年大廠Java崗面試必問,
  7. En tant que programmeur, quelle est la plus grande tristesse que vous ressentez? L'entrevue d'emploi Java de 2021 dans une grande usine vous demandera:
  8. Comme vous devriez le savoir en tant qu'intervieweur Java, 2021 dernier résumé des bibliothèques open source couramment utilisées pour Java,
  9. En tant que programmeur, je n'oublie pas le dernier résumé de mon expérience d'entrevue de stage en Java.
  10. 作為一名Java面試者你應該知道的,2021最新Java常用開源庫總結,
  11. New feature of Java 8. Stream (). Map (general programming method: collect. Groupingby)
  12. Computer graduation project java + SSM hospital registration system
  13. 作為一名程序員我不忘初心,Java最新實習面試經驗總結,
  14. 使用Docker部署Spring-Boot项目,论程序员成长的正确姿势,
  15. Conseils pour améliorer l'efficacité du Code Java mille fois
  16. 全网首发,我在华为做Java外包的真实经历!
  17. 全套Java视频百度云,终于找到一个看得懂的JVM内存模型了,
  18. 入职3个月的Java程序员面临转正,字节跳动 京东 360 网易面试题整理,
  19. Docker tutorial series (I) introduction to docker tutorial spring cloud mybatis distributed microservice Cloud Architecture
  20. 全網首發,我在華為做Java外包的真實經曆!
  21. Lancement de l'ensemble du réseau, je fais l'expérience réelle de l'externalisation Java à Huawei!
  22. Run around with money? Li Weijia fell into the storm of endorsement! In the face of collective hot discussion, personal attitude has become the focus of attention
  23. 全套Java視頻百度雲,終於找到一個看得懂的JVM內存模型了,
  24. Un ensemble complet de vidéos Java Baidu Cloud a finalement trouvé un modèle de mémoire JVM compréhensible.
  25. Déployez le projet Spring Boot avec docker, et parlez de la bonne posture pour que les programmeurs grandissent.
  26. 关于网络优化你必须要知道的重点,GC 堆排 Tomcat 算法题,
  27. 关于电商秒杀系统中防超卖处理方案简述,Java开发热门前沿知识,
  28. Les programmeurs Java qui sont entrés dans l'entreprise pendant trois mois ont dû faire face à une correction d'échelle, et les octets ont sauté dans le traitement des questions d'entrevue de JD 360 Netease.
  29. What is the new syntax of XX ≠ null in Java?
  30. Spring scheduled task cron expression (@ scheduled)
  31. Une brève description du plan de traitement anti - surproduction dans le système d'arrêt du commerce électronique et les connaissances de pointe du développement Java.
  32. Ce que vous devez savoir sur l'optimisation du réseau, c'est que le problème de l'algorithme Tomcat de gerbage GC,
  33. 凭借这份Java面试题集,成体系化的神级Java进阶笔记,
  34. 凭借这份Java面试题集,BAT大厂面试基础题集合,
  35. Docker Knowledge point collation
  36. Redis sur la réalisation élégante des tâches retardées
  37. 憑借這份Java面試題集,BAT大廠面試基礎題集合,
  38. Avec cet ensemble de questions d'entrevue Java, l'ensemble de questions de base d'entrevue de bat,
  39. Avec cet ensemble de questions d'entrevue Java, les notes avancées Java de niveau divin sont systématisées,
  40. Opérateurs arithmétiques et opérateurs de comparaison pour JavaScript, Introduction classique au développement web
  41. MySQL + +: slow query log analysis (I)
  42. Android Architect path 21 Responsive Programming RX Java thread transformation Principles
  43. Explorer le cadre open source Android - 1. Okhttp Source Analysis
  44. 分布式宝典:限流 缓存 通讯,Java开发中常见的一些问题面试专题,
  45. 分享面试经历的网站,腾讯大牛教你自己写Java框架!
  46. Expliquer les six principes de base du modèle de conception par des exemples réels
  47. Site Web pour partager vos expériences d'entrevue, Tencent Bull vous apprend à écrire votre propre cadre Java!
  48. Dictionnaire distribué: communication de cache limitée par le courant, sujets d'entrevue pour certaines questions courantes dans le développement Java,
  49. Another uncle circle man is angry! The high-quality acting skills make people admire and achieve the highlight of the ending of spring in Jade House
  50. 10. MySQL database import, export and authorization
  51. 9. MySQL data query
  52. 8. MySQL data operation DML
  53. 7. MySQL database table engine and character set
  54. 分享面試經曆的網站,騰訊大牛教你自己寫Java框架!
  55. Les points de connaissance de Java Real - time Video Download, Byte Jumping Java R & D post ont été divulgués à l'intérieur.
  56. Introduction au JavaScript chapitre 15 (objets, clairvoyance)
  57. 前方高能,Java程序员最大的悲哀是什么?
  58. Tencent private cloud MySQL solution tdsql
  59. 前方高能,Java程序員最大的悲哀是什麼?
  60. Quelle est la plus grande tristesse des programmeurs Java à l'avenir?