1.安装crontab
yum install crontabs 默认已安装
systemctl enable crond (设为开机启动)
systemctl start crond(启动crond服务)
systemctl status crond (查看状态)
2.设置定时任务
crontab -l
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
vim /etc/crontab
0 * / 2 * * * / sbin/service httpd restart 意思是每两个小时重启一次apache
50 7 * * * / sbin/service sshd start 意思是每天7:50开启ssh服务
50 22 * * * / sbin/service sshd stop 意思是每天22:50关闭ssh服务
0 0 1 , 15 * * fsck / home 每月1号和15号检查 / home 磁盘
1 * * * * / home/bruce/backup 每小时的第一分执行 / home/bruce/backup这个文件
00 03 * * 1-5 find / home "*.xxx" - mtime + 4 - exec rm {
} \ ; 每周一至周五3点钟,在目录 / home中,查找文件名为 * . xxx的文件,并删除4天前的文件。
30 6 * / 10 * * ls 意思是每月的1、11、21、31日是的6:30执行一次 ls 命令
3.保存
# 加载任务使其生效 (每次改动都需要执行,否则定时任务不生效)
crontab /etc/crontab
# 查看任务
crontab -l
# 直接编辑
vim /etc/crontab;
[2023-02-04 22:34:23.0 牵马的蜘蛛修改过此文章!]