作者:mce | 来源:互联网 | 2023-05-17 16:08
IhaveagentooLinuxsystemandastrangebehaviorofcrontab.Asrootuser,andasIunderstandth
I have a gentoo Linux system and a strange behavior of crontab. As root
user, and as I understand the documentation, the command
我有一个gentoo Linux系统和crontab的奇怪行为。作为root用户,以及我理解的文档,命令
crontab -l
lists all crontab jobs defined for the current user, root
(There are no cronjobs defined for any other used). All listed cronjobs are also defined in the file /etc/cronjob
.
列出为当前用户root定义的所有crontab作业(没有为任何其他使用的cronjobs定义)。所有列出的cronjobs也在文件/ etc / cronjob中定义。
However, there are two more crontab
files located in /etc/cron.d
, which define a cronjob each:
但是,在/etc/cron.d中还有两个crontab文件,它们分别定义了一个cronjob:
/etc/cron.d/testcron1
/etc/cron.d/testcron2
Although not listed with crontab -l
, the cronjob defined in the file /etc/cron.d/testcron1
is executed. The other cronjob defined in the file /etc/cron.d/testcron2
is NOT executed.
虽然未与crontab -l一起列出,但执行文件/etc/cron.d/testcron1中定义的cronjob。文件/etc/cron.d/testcron2中定义的另一个cronjob未执行。
This all does not make sense, so I have two questions:
这一切都没有意义,所以我有两个问题:
- Why does
crontab -l
list not all cronjobs?
- 为什么crontab -l列出不是所有的cronjobs?
- Must the cronjobs in
/etc/cron.d
be registered somewhere, or is a restart of a daemon/service required? Why is the one started, and not the other one (the executable works fine, though).
- /etc/cron.d中的cronjobs必须在某处注册,还是需要重新启动守护进程/服务?为什么一个启动,而不是另一个(可执行文件工作正常)。
1 个解决方案
1
The command crontab is used to maintain/manage crontab files for individual users. These files are usually located in /var/spool/cron/crontabs
.
命令crontab用于维护/管理单个用户的crontab文件。这些文件通常位于/ var / spool / cron / crontabs中。
If crontab -l
dose not show any cron jobs then this user currently has no individual cron jobs. This dose not mean that there are no cron jobs in /etc/cron* taht will run with the privileges of this user. crontab
will not operate on the files in /etc/cron*. It is a tool for only managing individual (per user) cron jobs held in /var/spool/cron/crontabs
.
如果crontab -l不显示任何cron作业,则此用户当前没有单独的cron作业。这并不意味着/ etc / cron * taht中没有cron作业将以该用户的权限运行。 crontab不会对/ etc / cron *中的文件进行操作。它是一个仅用于管理/ var / spool / cron / crontabs中保存的个人(每个用户)cron作业的工具。
Now lets see how the different cron jobs get executed. Form the manpage of the cron daemon we can read:
现在让我们看看如何执行不同的cron作业。形成cron守护进程的联机帮助页,我们可以阅读:
cron searches its spool area (/var/spool/cron/crontabs) for crontab files (which are named after accounts in /etc/passwd); ...
cron在其假脱机区域(/ var / spool / cron / crontabs)中搜索crontab文件(以/ etc / passwd中的帐户命名); ...
cron also reads /etc/crontab, which is in a slightly different format (see crontab(5)).
cron还读取/ etc / crontab,它的格式略有不同(参见crontab(5))。
as well as:
以及:
Additionally, in Debian, cron reads the files in the /etc/cron.d directory. cron treats the files in /etc/cron.d as in the same way as the /etc/crontab file...
另外,在Debian中,cron读取/etc/cron.d目录中的文件。 cron以与/ etc / crontab文件相同的方式处理/etc/cron.d中的文件...
(I think this applies to gentoo as well...)
(我认为这也适用于gentoo ...)
About restarting we can read:
关于重启我们可以阅读:
cron then wakes up every minute, examining all stored crontabs, checking each command to see if it should be run in the current minute...
然后cron每分钟醒来,检查所有存储的crontabs,检查每个命令,看它是否应该在当前分钟运行...
Additionally, cron checks each minute to see if its spool directory's modtime (or the modtime on the /etc/crontab file) has changed, and if it has, cron will then examine the modtime on all crontabs files and reload those which have changed. Thus cron need not be restarted whenever a crontab file is modified. Note that the crontab(1) command updates the modtime of the spool directory whenever it changes a crontab.
另外,cron检查每一分钟以查看其假脱机目录的modtime(或/ etc / crontab文件中的modtime)是否已更改,如果有,cron将检查所有crontabs文件的modtime并重新加载已更改的那些。因此,无论何时修改crontab文件,都不需要重新启动cron。请注意,只要crontab(1)命令更改了crontab,它就会更新假脱机目录的modtime。
So the crontab
command is for user specific corn jobs while the files in /etc/cron*
are more for system cron jobs.
因此,crontab命令用于特定于用户的玉米作业,而/ etc / cron *中的文件则用于系统cron作业。
No manual triggering is needed to activate a new cron job.
激活新的cron作业不需要手动触发。