1.使用crontab配置定时任务
crontab是一个配置工具,用来配置每一个登录用户的定时任务
是一张表,可以配置多个,但是删除的时候,删除的是这个用户所有的定时任务(就是你用crontab配置的所有定时)
crontab -e #编辑定时任务 相同用户编辑的都是同一个 也只有一个(文件)
crontab -l #查询用户下所有的crontab定时任务
crontab -r #删除用户下的crontab
执行
crontab -e 第一次执行的时候出现,说明系统还没为登录用户创建过任务表
我这里选择了2 用vim


/var/spool/cron/crontabs下的文件不要手动编辑
最好用crontab -e 来编辑
* * * * * echo "Hello crontab" >> /usr/local/develop/crontab.log
每分钟往文件输入
crontab -l 查看定时任务
查看日志文件变化
tail /usr/local/develop/crontab.log
crontab -r 属于该登录用户的crontab定时任务文件也就不见了

cron是crontab的守护进程 由cron来执行crontab
systemctl status cron 默认系统重启会自动重启cron
一般来说不需要去管cron cron系统自己会管
# 启动
sudo systemctl start cron
# 停止
sudo systemctl stop cron
# 重启
sudo systemctl restart cron
# 设置开机自启(默认已经是enabled)
sudo systemctl enable cron
2.使用systemd Timer定时任务
systemd提供比crontab更加强大
2.1 创建xxx.service放在 /lib/systemd/system/下面 例如 /lib/systemd/system/xxx.service
2.2 创建定时器文件 在/lib/systemd/system/下面创建一个xxx.timer
vim /lib/systemd/system/hellocron.service
[Unit]
# 服务描述,用于识别这个服务的用途
Description=Hello cron service
[Service]
# 服务类型:oneshot 表示执行一次就退出的服务(适合定时任务)
Type=oneshot
# 实际要执行的命令
# /bin/bash -c 表示使用bash shell执行后面的命令
ExecStart=/bin/bash -c 'echo "Hello cron" >> /usr/local/develop//logfile.log'
# 可选:指定运行此服务的用户(避免权限问题)
# User=your_username
# 可选:指定工作目录
# WorkingDirectory=/path/to/working/dir
vim /lib/systemd/system/hellocron.timer
[Unit]
# 定时器描述,说明定时器的用途和执行时间
Description=Run Hello cron service every minute
[Timer]
# 执行时间计划:每分钟执行一次
# 方法一:使用日历格式(推荐)
OnCalendar=*:*:00
# 或者方法二:使用相对时间
# OnBootSec=60
# OnUnitActiveSec=60
# 持久化设置:如果错过执行时间(如系统关机),在下次启动时立即执行
Persistent=true
# 可选:随机延迟时间(秒),避免多个定时器同时启动
# RandomizedDelaySec=10
# 可选:定时器精度(默认1分钟),设置为更精确的值
AccuracySec=1s
[Install]
# 安装目标:表示这个定时器属于 timers.target
# 系统启动时会自动启动这个定时器
WantedBy=timers.target
# 重新加载 systemd 配置
sudo systemctl daemon-reload
# 检查状态 默认没有启动
sudo systemctl status hellocrond.timer
# 启用定时器(设置开机自启)
sudo systemctl enable hellocron.timer
# 启动定时器
sudo systemctl start hellocron.timer
# 再次检查状态
sudo systemctl status hellocron.timer

删除 systemd timer
# 停止定时器
sudo systemctl stop hellocron.timer
# 禁用定时器(移除开机自启)
sudo systemctl disable hellocron.timer
删除相关文件
# 删除定时器文件
sudo rm /lib/systemd/system/hellocron.timer
# 删除服务文件(如果不再需要)
sudo rm /lib/systemd/system/hellocron.service
重新加载systemd
# 重新加载 systemd 配置
sudo systemctl daemon-reload
# 查看所有定时器
sudo systemctl list-timers
完整删除流程
sudo systemctl stop hellocron.timer
sudo systemctl disable hellocron.timer
sudo rm /lib/systemd/system/hellocron.timer
sudo rm /lib/systemd/system/hellocron.service
sudo systemctl daemon-reload

3.关于/etc/crontab/和/etc/cron.d/
1. /etc/crontab - 直接编辑
sudo nano /etc/crontab
例如
# 分钟 小时 日期 月份 星期几 用户名 命令
*/5 * * * * root echo "每5分钟执行" >> /var/log/mycron.log
0 2 * * * root /usr/local/bin/backup.sh
2. /etc/cron.d/ - 创建独立文件
# 创建新的cron任务文件
sudo nano /etc/cron.d/my-app-tasks
*/10 * * * * www-data /usr/bin/php /var/www/app/cleanup.php
0 1 * * * root /usr/local/bin/daily-report.sh
3.其他相关 直接写按需在下面这些定时任务里面
-
/etc/cron.hourly/
- 每小时执行的脚本 -
/etc/cron.daily/
- 每天执行的脚本 -
/etc/cron.weekly/
- 每周执行的脚本 -
/etc/cron.monthly/
- 每月执行的脚本

# 查看系统 cron 日志
sudo tail -f /var/log/syslog | grep cron
# 检查 cron 服务状态
sudo systemctl status cron
-
任何对
/etc/crontab
、/etc/cron.d/
等文件的修改都会立即被检测到 -
不需要手动重启或重新加载服务