crontab -e 配置定时任务

本文详细介绍了CronTab的使用方法,包括如何编辑定时任务文件,设置各种时间格式,如每分钟、每五分、每天等,并提供了具体实例。此外,还列举了特殊时间配置,如工作日、每月初等,以及相关参考资料。

时间格式

在这里插入图片描述

crontab -e 具体配置

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m       h       dom    mon  dow    command
# 分钟    小时     某天    某月  星期几  命令

0 23 * * * /root/scripts/login_day.sh
0 23 * * 7 /root/scripts/login_week.sh
0 0 1 * *  /root/scripts/login_month.sh

# total used time(hours) of every province for everyday
0 23 * * * /root/scripts/day_total_used_time.sh



examples

Every Minute
* * * * *
Every Five Minutes
*/5 * * * *
Every 10 Minutes
*/10 * * * *
Every 15 Minutes
*/15 * * * *
Every 30 Minutes
*/30 * * * *
Every Hour
0 * * * *
Every Two Hours
0 */2 * * *
Every Six Hours
0 */6 * * *
Every 12 Hours
0 */12 * * *
During the Work Day
*/5 9-17 * * *
Every day at Midnight
0 0 * * *
Every Two Weeks
0 0 * * Sun [ $(expr $(date +%W) % 2) -eq 1 ] && /path/to/command
At the Start of Every Month
0 0 1 * *
On January 1st at Midnight
0 0 1 1 *

References

  1. https://pubs.opengroup.org/onlinepubs/9699919799/utilities/crontab.html#tag_20_25_07
  2. https://crontab.guru/examples.html
使用 `crontab -e` 命令可以编辑当前用户的定时任务列表。该命令会打开一个文本编辑器,用户可以在其中添加、修改或删除定时任务条目。以下是详细的步骤和示例: ### 编辑 crontab 文件 1. **打开 crontab 编辑器**: 在终端中输入以下命令来编辑当前用户的 crontab 文件: ```bash crontab -e ``` 这将打开默认的文本编辑器(通常是 `nano` 或 `vi`),你可以在这个文件中添加或修改定时任务。 2. **添加定时任务**: 在编辑器中,按照 cron 的语法格式添加一行新的定时任务。每一行代表一个任务,语法如下: ``` * * * * * command_to_be_executed ┬─────┬─────┬─────┬─────┬───── │ │ │ │ │ │ │ │ │ └───── 星期几 (0 - 6) (星期天为0) │ │ │ └─────────── 月份 (1 - 12) │ │ └───────────────── 日期 (1 - 31) │ └─────────────────────── 小时 (0 - 23) └───────────────────────────── 分钟 (0 - 59) ``` 例如,要每天凌晨4点30分执行 `/home/user/backup.sh` 脚本,可以添加如下行: ``` 30 4 * * * /home/user/backup.sh ``` 3. **保存并退出**: 完成编辑后,保存文件并退出编辑器。如果你使用的是 `nano`,可以通过按 `Ctrl+O` 保存,然后按 `Ctrl+X` 退出;如果使用的是 `vi`,则输入 `:wq` 并按回车键保存并退出。 4. **验证任务是否生效**: 你可以通过运行以下命令查看当前用户的 crontab 内容,确认新添加的任务是否已经存在: ```bash crontab -l ``` ### 示例 假设你有一个名为 `daily_cleanup.sh` 的脚本,位于 `/home/user/scripts/` 目录下,并且希望它在每周一早上9点执行一次,可以在 crontab 中添加如下行: ``` 0 9 * * 1 /home/user/scripts/daily_cleanup.sh ``` ### 注意事项 - **环境变量**:cron 执行任务时使用的环境变量可能与你的 shell 环境不同,因此建议在脚本中显式设置所需的路径和环境变量。 - **权限问题**:确保脚本具有可执行权限。可以通过以下命令赋予执行权限: ```bash chmod +x /path/to/script ``` - **日志记录**:为了调试方便,可以在脚本中添加日志记录功能,或者将输出重定向到日志文件。例如: ```bash 0 9 * * 1 /home/user/scripts/daily_cleanup.sh >> /home/user/logs/cleanup.log 2>&1 ``` 通过以上步骤,你可以轻松地使用 `crontab -e` 配置定时任务[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值