liunx定时任务,centos定时任务

在这里插入图片描述

yum install cronie crontabs -y

直接运行
crond -n 在前台运行
crond -i 守护进程在没有inotify支持的情况下运行

systemctl service crond start    # 启动服务
systemctl enable crond.service   # 设置开机自启
sudo systemctl restart crond # 重启 cron 服务

systemctl service crond.service   # 检查服务是否正在运行

crontab -l    # 列出当前用户的定时任务(首次运行可能显示 "no crontab for root")
crontab -e    # 编辑定时任务
crontab -r    # 删除所有定时任务
* * * * * command_to_execute
五个星号分别代表:分钟(0-59)、小时(0-23)、日期(1-31)、月份(1-12)、星期(0-707 都代表周日)。

每天凌晨 2 点执行 backup.sh 脚本:
0 2 * * * /path/to/backup.sh

每小时的第 15 分钟执行 logrotate:
15 * * * * /usr/sbin/logrotate /etc/logrotate.conf

10秒执行一次,如果不需要严格的 10 秒间隔,也可以通过错开多个 cron 任务实现近似效果:
* * * * * /path/to/your_command
* * * * * (sleep 10; /path/to/your_command)
* * * * * (sleep 20; /path/to/your_command)
* * * * * (sleep 30; /path/to/your_command)
* * * * * (sleep 40; /path/to/your_command)
* * * * * (sleep 50; /path/to/your_command)

或者编写run_every_10s
#!/bin/bash
for i in {1..6}; do
    /path/to/your_command  # 替换为你的实际命令
    sleep 10
done
添加可执行权限
chmod +x /usr/local/bin/run_every_10s.sh
编写定时任务
crontab -e
编写
* * * * * /usr/local/bin/run_every_10s.sh


写日记

# 每分钟向 /tmp/cron_test.txt 写入当前时间
* * * * * date >> /tmp/cron_test.txt 2>&1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大得369

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值