crontab计划定时任务

﹏ヽ暗。殇╰゛Y 2022-05-16 07:52 385阅读 0赞

参数简介:

Crontab –u root #指定root用户的cron服务

Crontab -l 列出当前用户下 cron服务

Crontab –r #删除crontab服务

Crontab -e 编辑cront 服务

*代表取值 范围内的数字 任意

/ 指定时间的间隔频率 */10 0-23/2

查看创建计划的用户

/var/spool/cron/

Crontab格式

Vim /etc/crontab #分,时,日,月,周 命令

查看服务状态

Systemctl status crond

[root@localhost ~]# crontab -l

36 23 * * * /root/a.sh

01 08 * * * echo “hello” > /root/a.txt

实例:

每天2:00备份/etc/目录到/tmp/backup下面

将备份命令写入一个脚本中

每天备份文件名要求格式: 2017-08-19_etc.tar.gz

在执行计划任务时,不要输出任务信息

存放备份内容的目录要求只保留三天的数据

====================================================

mkdir /tmp/backup

tar zcf etc.tar.gz /etc

find /tmp/backup -name “***.tar.gz**” -mtime +3 -exec rm -rf {}\;

============================================================

[root@test ~]# crontab -l

13 21 * * * echo “hello” > /tmp/a.txt

0 22 * * * /root/backup.sh &> /dev/null

[root@test ~]# cat backup.sh

#!/bin/bash

find /tmp/backup -name “*.tar.gz” -mtime +3 -exec rm -f {}\;

#find /tmp/backup -name “*.tar.gz” -mtime +3 -delete

#find /tmp/backup -name “*.tar.gz” -mtime +3 |xargs rm -f

tar zcf /tmp/backup/`date +%F`_etc.tar.gz /etc

发表评论

表情:
评论列表 (有 0 条评论,385人围观)

还没有评论,来说两句吧...

相关阅读

    相关 crontab定时任务

    一、用途 定时运行一个shell或者Linux脚本(可能还有其他的用途 暂时介绍这个) 二、如何用? a.在linux中 输入命令  crontab -e   执行之后