使用Cronjobs的综合指南

There are times when there’s a need for running a group of tasks automatically at certain times in the future. These tasks are usually administrative, but could be anything – from making database backups to downloading emails when everyone is asleep.

有时候,将来有必要在某些时候自动运行一组任务。 这些任务通常是管理性的,但可能是任何事情–从进行数据库备份到每个人都睡着时下载电子邮件。

Cron is a time-based job scheduler in Unix-like operating systems, which triggers certain tasks at a point in the future. The name originates from the Greek word χρόνος (chronos), which means time.

Cron是类Unix操作系统中的基于时间的作业调度程序,它将在将来的某个时刻触发某些任务。 名称源自希腊语χρόνος (chronos),意思是时间。

The most commonly used version of Cron is known as Vixie Cron, originally developed by Paul Vixie in 1987.

最常用的Cron版本称为Vixie Cron,最初由Paul Vixie于1987年开发。

This article is an in-depth walkthrough of this program, and a reboot of this ancient, but still surprisingly relevant post.

本文是该程序的深入演练,也是该古老但仍然出乎意料的相关文章的重新启动。

Chronos Image

术语 (Terminology)

  • Job: a unit of work, a series of steps to do something. For example, sending an email to a group of users. In this article, we’ll use task, job, cron job or event interchangeably.

    工作:一个工作单元,一系列要做某事的步骤。 例如,向一组用户发送电子邮件。 在本文中,我们将交替使用taskjobcron jobevent

  • Daemon: (/ˈdiːmən/ or /ˈdeɪmən/) is a computer program which runs in the background, serving different purposes. Daemons are often started at boot time. A web server is a daemon serving HTTP requests. Cron is a daemon for running scheduled tasks.

    守护程序: (/ ˈdiːmən /或/ ˈdeɪmən /)是在后台运行的计算机程序,可用于不同目的。 守护程序通常在引导时启动。 Web服务器是服务于HTTP请求的守护程序。 Cron是用于运行计划任务的守护程序。

  • Cron Job: a cron job is a scheduled job, being run by Cron when it’s due.

    Cron作业: cron作业是计划的作业 ,在到期时由Cron运行。

  • Webcron: a time-based job scheduler which runs within the web server environment. It’s used as an alternative to the standard Cron, often on shared web hosts that do not provide shell access.

    Webcron:在Web服务器环境中运行的基于时间的作业计划程序。 它通常用作标准Cron的替代品,通常在不提供外壳程序访问权限的共享Web主机上使用。

入门 (Getting Started)

This tutorial assumes you’re running a Unix-based operating system like Ubuntu. If you aren’t, we recommend setting up Homestead Improved – it’s a 5 minute process which will save you years down the line.

本教程假定您正在运行基于Ubuntu的基于Unix的操作系统。 如果不是这样,我们建议您设置Homestead Improvement –这是一个5分钟的过程,可以节省您数年的时间。

If we take a look inside the /etc directory, we can see directories like cron.hourly, cron.daily, cron.weekly and cron.monthly, each corresponding to a certain frequency of execution. One way to schedule our tasks is to place our scripts in the proper directory. For example, to run db_backup.php on a daily basis, we put it inside cron.daily. If the folder for a given frequency is missing, we would need to create it first.

如果在/etc目录中查看,我们会看到cron.hourlycron.dailycron.weeklycron.monthly /etc目录,每个目录都对应于一定的执行频率。 安排任务的一种方法是将脚本放置在正确的目录中。 例如,要每天运行db_backup.php ,请将其放入cron.daily 。 如果缺少给定频率的文件夹,则需要首先创建它。

Note: This approach uses the run-parts script, a command which runs every executable it finds within the specified directory.

注意:此方法使用run-parts脚本,该命令运行在指定目录中找到的每个可执行文件。

This is the simplest way to schedule a task. However, if we need more flexibility, we should use Crontab.

这是安排任务的最简单方法。 但是,如果需要更大的灵活性,则应使用Crontab。

Crontab文件 (Crontab Files)

Cron uses special configuration files called crontab files, which contain a list of jobs to be done. Crontab stands for Cron Table. Each line in the crontab file is called a cron job, which resembles a set of columns separated by a space character. Each row specifies when and how often a certain command or script should be executed.

Cron使用称为crontab文件的特殊配置文件,其中包含要完成的作业列表。 Crontab代表Cron Table 。 crontab文件中的每一行都称为cron作业,它类似于一组由空格分隔的列。 每行指定应在何时以及多久执行一次特定命令或脚本。

In a crontab file, blank lines or lines starting with #, spaces or tabs will be ignored. Lines starting with # are considered comments.

在crontab文件中,空白行或以#开头的行,空格或制表符将被忽略。 以#开头的行被视为注释。

Active lines in a crontab are either the declaration of an environment variable or a cron job, and comments are not allowed on the active lines.

crontab中的活动行是环境变量的声明或cron作业,并且活动行中不允许注释。

Below is an example of a crontab file with just one entry:

以下是仅包含一个条目的crontab文件的示例:

0 0 * * *  /var/www/sites/db_backup.sh

The first part 0 0 * * * is the cron expression, which specifies the frequency of execution. The above cron job will run once a day.

第一部分0 0 * * *是cron表达式,它指定执行频率。 上面的cron作业每天运行一次。

Users can have their own crontab files named after their username as registered in the /etc/passwd file. All user-level crontab files reside in Cron’s spool area. These files should not be edited directly. Instead, we should edit them using the crontab command-line utility.

用户可以使用在/etc/passwd文件中注册的用户名命名自己的crontab文件。 所有用户级crontab文件都位于Cron的假脱机区域中。 这些文件应该被直接编辑。 相反,我们应该使用crontab命令行实用工具对其进行编辑。

Note: The spool directory varies across different distributions of Linux. On Ubuntu it’s /var/spool/cron/crontabs while in CentOS it’s /var/spool/cron.

注意:假脱机目录在Linux的不同发行版中有所不同。 在Ubuntu上是/var/spool/cron/crontabs而在CentOS中是/var/spool/cron

To edit our own crontab file:

要编辑我们自己的 crontab文件:

crontab -e

The above command will automatically open up the crontab file which belongs to our user. If a default system editor for the crontab hasn’t been selected before, a choice will be presented listing the installed ones. We can also explicitly choose or change our desired editor for editing the crontab file:

上面的命令将自动打开属于我们用户的crontab文件。 如果以前没有为crontab选择默认的系统编辑器,则会显示一个选项,列出已安装的程序。 我们还可以显式选择或更改所需的编辑器来编辑crontab文件:

export VISUAL=nano; crontab -e

After we save the file and exit the editor, the crontab will be checked for accuracy. If everything is set properly, the file will be saved to the spool directory.

保存文件并退出编辑器后,将检查crontab的准确性。 如果一切设置正确,文件将保存到假脱机目录。

Note: Each command in the crontab file is executed from the perspective of the user who owns the crontab, so if your command runs as root (sudo) you will not be able to define this crontab from your own user account unless you log in as root.

注意: crontab文件中的每个命令都是从拥有crontab的用户的角度执行的,因此,如果您以root(sudo)身份运行命令,则除非您以根。

To list the installed cron jobs belonging to our own user:

列出属于我们自己用户的已安装cron作业:

crontab -l

We can also write our cron jobs in a file an

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值