为什么我的 cron 作业没有被安排?

Tim*_*Tim 2 cron

我正在尝试通过创建一个文件来设置一个 cron 作业/etc/cron.d/myjob

55 * * * * t echo hello > /tmp/cron.log  && cd /tmp/test/ && pwd > /tmp/cron.log
Run Code Online (Sandbox Code Playgroud)

我尝试使用重定向来验证作业是否已成功安排。我在 21:50 创建了文件,当 5 分钟后即 21:55 时,仍然没有创建 /tmp/cron.log。我想知道为什么?

我指定了要t/etc/cron.d/myjob. 但谁是 cron 工作?我不确定,所以我尝试了下面的两个命令。都没有显示我创建的工作。

$ crontab -l
no crontab for t
$ sudo crontab -l
[sudo] password for t: 
no crontab for root
Run Code Online (Sandbox Code Playgroud)

我的 /etc/crontab 没有从 /etc/cron.d/ 下的文件中明确读取。见下文。这可能是我的 cron 作业没有运行的原因吗?谢谢。

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
Run Code Online (Sandbox Code Playgroud)

更新:

感谢杰夫。更改权限/etc/cron.d/myjobrw-r--r--解决问题。为什么rw-rw-r--我的作业文件的原始权限不起作用,而是rw-r--r--必需的?做下的文件/etc/cron.d//etc/cron.daily/必须具有相同的权限rw-r--r--

为什么/etc/crontab不需要显式读取 下的文件/etc/cron.d/

ImH*_*ere 6

第一季度

为什么原来的 rw-rw-r-- 我的作业文件的权限不起作用

来自man cron

/etc/crontab 和 /etc/cron.d 中的文件必须由 root 拥有,并且不能是组或其他可写的。

所以,里面的文件/etc/cron.d应该是:

chown root:root /etc/cron.d/*
chmod go-wx /etc/cron.d/*
chmod -x /etc/cron.d/*
Run Code Online (Sandbox Code Playgroud)

Q2

为什么 /etc/crontab 不需要显式读取 /etc/cron.d/ 下的文件?

不,这不是它所说的。什么man 5 crontab说的是:

/etc/crontab: 系统范围的 crontab 与任何其他 crontab 不同,当您编辑此文件和 /etc/cron.d 中的文件时,您不必运行 `crontab' 命令来安装新版本。这些文件也有用户名字段,其他 crontab 都没有。

这意味着不需要运行crontab -e来编辑和安装新的 cron 作业。编辑后立即/etc/cron.d/读取和解释下面的文件cron。并且其中定义的作业按计划执行,而无需调用crontab可执行文件。

警告

但你也应该阅读man cron

一般来说,系统管理员不应该使用/etc/cron.d/,而是使用标准的系统crontab /etc/crontab。

这意味着执行 acrontab -e以创建 cron 作业。

直接编辑/etc/crontab(或/etc/cron.d文件)一般来说是一个坏主意,特别是对于新用户。习惯于管理与cron作业crontab -ecrontab -l,然后,经过一番(长)的时候,你可能会试着去了解什么/etc/crontab呢。

有关的


编辑

既然你问:

crontab -e 可以在 /etc/cron.d/ 中创建 cron 作业吗?

不,我不是这个意思。Cron 作业(针对用户)存在于/var/spool/cron/crontabs(在类似 debian 的发行版中)。这就是命令crontab -e编辑它们的地方。

通常被编辑(之前)的/etc/crontabsystem作业,安装包(如 anacron)用来安排自己的作业的作业。那不是用户应该编辑的文件。在 debian 中,有一个添加新作业的目录,可以避免编辑文件 ( /etc/crontab)出错的问题。该目录是/etc/cron.d/. 但是,这不是供用户(甚至管理员)手动编辑的目录。

这类似于询问用户为什么不应该编辑/etc/sudoers文件。

当然,用户(以 root 身份)可以执行nano /etc/sudoers和更改文件。系统不会阻止root。但这是一个“坏主意”。

crontab 的手册具有误导性,因为它是为了与开发人员(而不是用户)交谈而编写的。它解释了开发人员应该在这些文件中做什么。

为什么

关于:为什么你认为你需要添加到/etc/crontab(或/etc/cron.d)?

这两个地方的文件包含“行”,每一行都是一个作业。
用 编辑的行完全相同crontab -e。每一行都是一个要由 cron 执行的作业。唯一的区别是在这两个地方,工作行包含一个额外的“用户”字段。但:

crontab -u user1 -e
Run Code Online (Sandbox Code Playgroud)

还将添加将作为“user1”执行的新行(作业)(如果运行该命令的用户具有正确的权限)。唯一的(实际)不同的是,用户可以编辑这些工作,但不是那些内部/etc/crontab/etc/cron.d/。这似乎是 IMO 避免用户编辑其他用户作业的好主意。

简而言之:添加作业:使用crontab -e.