liunx定时任务,centos定时任务

cpp 复制代码
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    # 删除所有定时任务
cpp 复制代码
* * * * * command_to_execute
五个星号分别代表:分钟(0-59)、小时(0-23)、日期(1-31)、月份(1-12)、星期(0-7,0 和 7 都代表周日)。

每天凌晨 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

写日记

cpp 复制代码
# 每分钟向 /tmp/cron_test.txt 写入当前时间
* * * * * date >> /tmp/cron_test.txt 2>&1
相关推荐
GuokLiu5 小时前
250708-Debian系统安装Edge浏览器并配置最小中文输入法
运维·edge·debian
Two_brushes.5 小时前
【linux 网络】网络基础
linux·网络
Code Warrior5 小时前
【Linux】基础开发工具(3)
linux·服务器
鬼才血脉5 小时前
Linux(centos)安装 MySQL 8
linux·mysql·centos
guygg886 小时前
ubuntu手动编译VTK9.3 Generating qmltypes file 失败
linux·运维·ubuntu
JeffersonZU6 小时前
Linux/Unix 套接字Socket编程(socket基本概念,流程,流式/数据报socket,Unix domain socket示例)
linux·c语言·tcp/ip·udp·unix·gnu
先做个垃圾出来………6 小时前
自动化一次通过率
运维·自动化
Two_brushes.7 小时前
【linux网络】网络编程全流程详解:从套接字基础到 UDP/TCP 通信实战
linux·开发语言·网络·tcp/udp
夕泠爱吃糖7 小时前
Linux中的静态库和动态库
linux·运维·服务器
比奥利奥还傲.8 小时前
Linux运维安全新范式:基于TCPIP与SSH密钥的无密码认证实战
linux·运维·安全