crontab -e与/ etc / crontab相比有何不同?


11

对于Unix和Cron来说,我还很陌生,我目前正试图将Cron添加到现有的Cron文件中。我读过你可以用做到这一点crontab -e。对我来说,令人困惑的是,它crontab -e显示的克朗/命令与less /etc/crontab-为何不同?哪一种是正确的编辑方式/文件?

Answers:


14

尽管@X Tian的答案包含有关crontab的不同文件的信息,但是有关您的问题的基本信息是:

crontab -e编辑用户的crontab文件(存储在/var/spool/cron/crontabs/当前Debian系统的目录中,但存储在YMMV中),或创建一个新文件,而不创建一个/etc/crontab。与crontab -l(列出crontab文件)和crontab -r(删除crontab文件)类似。

对于应在用户帐户下执行的所有cron作业,应使用crontab -e。对于系统作业,应该在下添加一个文件/etc/cron.d(如果存在);在下面/etc/cron.{hourly|daily|weekly|monthly}(但不能像包名一样命名!),如果它符合您的目的;或在中添加一行/etc/crontab。但是请注意,/etc/crontab系统更新可能会覆盖它。


@Stephane Chazelas感谢您澄清您的编辑。该/var/spool/cron/目录是Linux的专用目录(请参见文件层次结构标准),并且该位置在当前Debian以外的系统上可能有所不同。
2014年

3

您真的想阅读手册页man cronman crontab

以下摘录涵盖了您的问题。从man cron

注意cron在其假脱机区域(/ var / spool / cron / crontabs)中搜索crontab文件(以/ etc / passwd中的帐户命名);找到的crontab被加载到内存中。请注意,不应直接访问此目录中的crontabs-应该使用crontab命令访问和更新它们。

   cron also reads /etc/crontab, which is in a slightly  different  format
   (see  crontab(5)).   Additionally, cron reads the files in /etc/cron.d:
   it treats  the  files  in  /etc/cron.d  as  in  the  same  way  as  the
   /etc/crontab  file  (they  follow the special format of that file, i.e.
   they  include  the  user  field).  However,  they  are  independent  of
   /etc/crontab:  they  do  not, for example, inherit environment variable
   settings from it. The intended purpose of  this  feature  is  to  allow
   packages  that  require  finer  control  of  their  scheduling than the
   /etc/cron.{daily,weekly,monthly} directories to add a crontab  file  to
   /etc/cron.d. Such files should be named after the package that supplies
   them. Files must conform to the same naming convention as used by  run-
   parts(8):  they  must  consist solely of upper- and lower-case letters,
   digits, underscores, and hyphens. If the -l option is  specified,  then
   they must conform to the LSB namespace specification, exactly as in the
   --lsbsysinit option in run-parts.

1
您应该指定引用哪个操作系统的cron实现及其版本。cron操作系统之间的差异很大,在某些情况下,您可以在几种实现之间进行选择。
斯特凡Chazelas
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.