一、Execute Crond Service on openEuler

1 crond 概述

crond就是计划任务/定时任务

常见有闹钟、PC端定时关机 shutdown -s -t 200,定时执行

计划任务执行一些周期性的任务,夜深人静时,给服务器数据文件做定时备份数据,某个时间段有活动开启接口/关闭接口

使用情况

  • 临时文件清理、系统信息采集、日志文件切割
  • 定时向互联网同步时间,定时备份系统配置文件,定时备份数据库的数据

2 检查服务状态

## 检查是否安装
[root@ecs-65685 ~]# rpm -qf `which crond`
cronie-1.5.4-5.oe1.x86_64 ## 最小化服务已安装 crond,系统基础服务之一
systemctl status crond
##
systemctl enable crond
systemctl start crond
systemctl restart crond

3 crontab 配置文件记录时间周期的含义

## 定时任务的格式
[root@ecs-65685 ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root # For details see man 4 crontabs # Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed ## 帮助命令
[root@ecs-65685 ~]# man 4 crontabs [root@ecs-65685 ~]# crontab --help
crontab: invalid option -- '-'
crontab: usage error: unrecognized option
Usage:
crontab [options] file
crontab [options]
crontab -n [hostname] Options:
-u <user> define user
-e edit user's crontab
-l list user's crontab
-r delete user's crontab
-i prompt before deleting
-n <host> set host in cluster to run users' crontabs
-c get host in cluster to run users' crontabs
-s selinux context
-V print version and exit
-x <mask> enable debugging Default operation is replace, per 1003.2

4 定时任务规则/案例

## *  *  *  *  *  command
## 分  时  日  月 周 命令
## 1 表示分钟1~59 每分钟用*或者 */1表示
## 2 表示小时1~23 [0-23]
## 3 表示日期1~31
## 4 表示月份1~12 [jan,feb,mar,apr,...]
## 5 标识星期0~6 [sun,mon,tue,wed,thu,fri,sat]
## 6 运行的命 [df -h] ## * 任意时间 每 每分钟 每小时 每天 每周 每月
## /n 每隔/间隔多久执行一次
## ,[逗号] 分割时段独立时间
## -[减号] 区间范围 ## Go to bed at 10 every night
00 10 * * * sleep ## 每隔十分钟执行一次
*/10 * * * * cmd ## 每隔3小时检查磁盘使用率
00 */3 * * * df -h ## 每天 20,21,22 点的整点执行命令
00 20-22 * * * cmd ## 每天凌晨5和晚上20,22点的30分时执行命令
30 05,20,22 * * * cmd ## 下午14点到23点每两个小时执行命令
00 13-23/2 * * * cmd
00 14,16,18,20,22,00 * * * cmd ## 早上9点到11点和下午14点到20点,每2个小时执行命令
00 09-11,14-20/2 * * * cmd ## 每年的2月14日的3点执行命令
00 03 14 2 * cmd ## 每年 1,3,6月的每天凌晨2点执行命令
00 02 * 1,3,6 * cmd ## 每年的4,5,6月的周五的凌晨5点执行命令
00 05 * 4,5,6 5 cmd ## 不推荐同时书写日期和周几

二、Crontab 编写cron定时任务

参数 含义
-e 编辑定时任务
-l 当前用户的定时任务
-r 清空/删除当前用户的所有定时任务
-o 指定其他用户

1 每分钟执行内容写入文件中

## 配置定时任务文件/创建定时任务
crontab -e
## echo email to file
*/1 * * * * /usr/bin/echo iyuyi.xyz@aliyun.com &>> /tmp/xyz.txt ## 保存配置并即刻生效
crontab /etc/crontab ## 查看定时任务
crontab -l ## 测试与检查
tail -f /tmp/xyz.txt ## 检查定时任务的日志
tile -f /var/log/cron

2 每五分钟执行一次时间同步 ntpdate

##
dnf search ntp
dnf install ntpdate crontab -e
## ntpdate time
*/1 * * * * /usr/sbin/ntpdate ntp1.aliyun.com &> /dev/null
##
crontab -l
##
date

3

X、One Step Success

Y、Error message

Z、Related Links

最新文章

  1. 在DBeaver中phoenix查询报错:org.apache.phoenix.exception.PhoenixIOException: The system cannot find the path specified
  2. EF6 CodeFirst+Repository+Ninject+MVC4+EasyUI实践(五)
  3. Eclipse插件安装方式及使用说明
  4. POJ 2352 Stars(树状数组)
  5. 我曾经的第一个OC程序
  6. Linux中常用的查看系统信息的命令
  7. Dos del参数与作用(/f/s/q)
  8. 在XML里的XSD和DTD以及standalone的使用2----具体使用详解
  9. IBM发布AppScan Source 8.7:减少iOS企业级应用安全风险
  10. Notifications(通知)
  11. RDLC报表纵向合并单元格。
  12. 英特尔关闭PC计算卡项目—插个卡片就能升级个人电脑
  13. bzoj4198 荷马史诗
  14. JAVA学习笔记系列1-Java版本介绍
  15. zombodb sql functions 说明
  16. linux 高级字符设备驱动 ioctl操作介绍 例程分析实现【转】
  17. Android : Your APK does not seem to be designed for tablets.
  18. python函数作用域LEGB
  19. Linux配置python和pip环境
  20. (重要)LRU cache

热门文章

  1. 凭借SpringBoot整合Neo4j,我理清了《雷神》中错综复杂的人物关系
  2. Odoo自建应用初步总结(一)
  3. Kubernetes 监控:Prometheus Operator + Thanos ---实践篇
  4. Nginx+lua+openresty精简系列
  5. 8.第七篇 验证kube-apiserver及kubeconfig配置详解
  6. k8s中安装rabbitmq集群
  7. ImGUI 1.87 绘制D3D外部菜单
  8. C++面向对象编程之成员模板、模板特化、偏特化和模板模板参数
  9. Multi-View Intent Disentangle Graph Networks for Bundle Recommendation解读
  10. &#128293;支持 Java 19 的轻量级应用开发框架,Solon v1.10.4 发布