Im pretty new to unix and crons, I was currently about to try to add crons to an existing cron file. I read you could do this with crontab -e. The confusing thing to me is just that crontab -e shows different crons/commands than less /etc/crontab - how come? Which one is the correct way/file to edit?


Although @X Tian's answer contains info on the different files for crontab, the essential information concerning your question is this:

crontab -e edits the user's crontab file (stored in the /var/spool/cron/crontabs/ directory on current Debian systems, but YMMV) or creates a new one, and not /etc/crontab. Similar for crontab -l (list crontab file) and crontab -r (remove crontab file).

For all cron jobs that should be executed under a user's account, you should use crontab -e. For system jobs, you should add a file under /etc/cron.d, if that exists; under /etc/cron.{hourly|daily|weekly|monthly} (but those must not be named like a package name!), if that fits your purpose; or add a line to /etc/crontab. But be aware that /etc/crontab might be overwritten with a system update.

  • @Stephane Chazelas Thank you for clarifying with your edit. The /var/spool/cron/ directory is a Linux specialty (see the File Hierarchy Standard) and the location may vary on systems other than a current Debian. – Dubu Mar 13 '14 at 14:47

You really want to read the manual pages man cron and man crontab

Here is an extract which covers your question. From man cron

NOTES cron searches its spool area (/var/spool/cron/crontabs) for crontab files (which are named after accounts in /etc/passwd); crontabs found are loaded into memory. Note that crontabs in this directory should not be accessed directly - the crontab command should be used to access and update them.

   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
    You should specify which cron implementation and which version thereof on which OS and which version thereof you're quoting that from. cron is something that varies a lot from OS to OS and on some you can choose between several implementations. – Stéphane Chazelas Mar 13 '14 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.