Execute Crond Service on openEuler

2023-02-12,,,

一、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

Execute Crond Service on openEuler的相关教程结束。

《Execute Crond Service on openEuler.doc》

下载本文的Word格式文档,以方便收藏与打印。