编辑用户 crontab 定时任务
在编辑器中打开属于你的 cron 计划表,添加、修改或删除会自动定时执行的命令。
执行 crontab -e,添加一行如 30 2 * * * /home/user/backup.sh,即可每天 02:30 运行 backup.sh。
01命令
命令
$ crontab -e02作用说明
每行由五个时间字段(分、时、日、月、星期)加要执行的命令组成,'*' 表示「每一」,例如 0 4 * * 1 表示每周一 04:00 执行,*/15 * * * * 表示每 15 分钟执行一次。保存退出后 cron 会自动重新加载计划表,无需重启守护进程。用 crontab -l 查看当前任务;大多数发行版中 cron 会把命令输出发到用户邮箱,可查看 /var/mail/$USER 排查报错,或在命令后加重定向(>/dev/null 2>&1)来屏蔽输出。
03风险
!注意: cron 运行环境非常精简:脚本务必写绝对路径,不要依赖交互 shell 的 PATH 或环境变量。
04撤销
✓无需撤销——该命令不会改变持久状态。
05来源
- crontab(5) — tables for driving cron依据: The crontab(5) page describes the minute hour day month weekday command field format used in crontab files.
- crontab(1) — maintain crontab files for individual users依据: The crontab man page documents '-e' as editing the current user's crontab using the editor specified by VISUAL or EDITOR.
最后验证: 2026-09-17 · 分类: 系统与设置