如何使用 crontab 安排和管理任务

2025-06-07

如何使用 crontab 安排和管理任务

在详细介绍 crontab 之前,假设您正在运营一家网店,并且希望每周五上午 9 点(美国山地标准时间)发送一封关于最新优惠的电子邮件。那么该如何实现呢?当然,如果您有充足的时间,您可以编写自己的 n 行代码的作业调度程序,或者您也可以直接使用所有 Unix 和 Linux 操作系统中提供的 crontab 来安排任务。

什么是 Crontab

Crontab 是 cron table 的缩写。cron 是一个适用于所有 Linux 和 Unix 操作系统的实用程序,用于在给定的日期和时间运行任务或进程。因此,crontab 实际上是一张表,其中包含脚本或命令以及要运行的日期和时间。

如何查看 crontab 或 cron 表

在 Ubuntu 上,你可以使用 crontab -l 查看当前表

shaikh@shaikhu-com:~$ crontab -l
# 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

Enter fullscreen mode Exit fullscreen mode

使用 crontab -e 编辑和管理 cron 表

如何设置作业运行的日期和时间

以下是所有 Linux 系统支持的 crontab 格式。

# * * * * * command to be executed
# | | | | |
# | | | | |
# | | | | |
# | | | | |_______________Day of the week (0 - 6)(Sunday to Saturday)
# | | | |_______________Month (1 - 12)
# | | |_______________Day of the Month(1 - 31)
# | |_______________Hour(0 - 23)
# |_______________Minute(0 - 59)
Enter fullscreen mode Exit fullscreen mode

让我们通过一些例子来更好地理解

示例 1:让我们进入我们在开始时讨论的第一个示例,即
运行一项作业,每周五上午 9 点向所有订阅者发送电子邮件

0  9  *  *  5  /usr/bin/python3 sendEmail.py
Enter fullscreen mode Exit fullscreen mode

示例 2:在下面的示例中,我们每 15 分钟运行一次作业(注意/操作员)

*/15  *  *  *  *  doSomething.sh
Enter fullscreen mode Exit fullscreen mode

示例 3:这里我们每隔 1 小时和 5 小时运行一次作业(注意逗号)

*  1,5  *  *  *  doSomething.sh
Enter fullscreen mode Exit fullscreen mode

示例 4:以下作业于每年 1 月 1 日上午 7 点运行

0  7  *  1  *  happyNewYear.sh
Enter fullscreen mode Exit fullscreen mode

如果你想每小时随机运行一个作业怎么办

到目前为止,我们已经了解了如何使用 crontab 在特定日期和时间安排作业。但是,如果我们想在随机时间(例如每小时的随机分钟)运行作业,该怎么办呢?我们也可以通过编写如下所示的sleep命令来实现。

0  *  *  *  *  sleep $(($RANDOM%60))m;sh test.sh
Enter fullscreen mode Exit fullscreen mode

让我们理解一下我们上面做了什么。根据 crontab 规则,上面的命令集将每小时运行一次。每小时开始时,cron 会遇到两个命令,第一个命令是睡眠随机分钟数。所以这个 cron 会延迟这些随机分钟数,然后从睡眠状态唤醒后,执行第二个命令,也就是我们的工作 😃

如果您想做除了睡眠之外的更多的事情,我们可以编写一个 shell 脚本来代替睡眠。

如下所示,您可以在延迟 n 分钟后从 shell 脚本运行 python 脚本。

定时任务:

0  *  *  *  *  sh test.sh
Enter fullscreen mode Exit fullscreen mode

测试文件

#!/usr/bin/sh
#test.sh

TIME=$((RANDOM%60))
sleep "${TIME}m"
#Do some stuff
#Do some more stuff
/usr/bin/python3 /mybots/newsbot.py
Enter fullscreen mode Exit fullscreen mode

因此,基本上,在延迟随机分钟并执行更多代码操作后,您将从 shell 脚本运行一次 python 代码:)

结论

Cronjob 是一款非常实用的调度工具,用于安排任务。规则非常简单易记。你只需按照正确的格式运行任务或脚本即可。希望这能帮助你安排脚本/作业 :)。

文章来源:https://dev.to/shaikh/how-to-schedule-and-manage-tasks-using-crontab-20dj
PREV
我如何在一天内构建大约 60% 的应用程序代码库。
NEXT
使用 Python 的 NSE 期权链数据