27

System crontab:

/etc/crontab

Root crontab:

sudo crontab -u root -e

Which way is preferred? As they all run tasks within administration privilege.

peizhao
  • 551

2 Answers2

29

/etc/crontab is the system wide crontab.

The format of /etc/crontab is like this:

# m h dom mon dow user      command
*   *  *   *   *  someuser  echo 'foo'

while crontab -e is per user, it's worth mentioning with no -u argument the crontab command goes to the current users crontab. You can do crontab -e -u <username> to edit a specific users crontab.

Notice in a per user crontab there is no 'user' field.

# m h  dom mon dow  command
*   *   *   *   *   echo 'foo'

An aspect of crontabs that may be confusing is that root also has its own crontab. e.g. crontab -e -u root will not edit /etc/crontab See Configuring cron.

In Linux distros, per user crontabs are typically stored in: /var/spool/crontabs/<username>

References

https://superuser.com/questions/290093/difference-between-etc-crontab-and-crontab-e

Ramesh
  • 39,297
  • 4
    As Congiruring cron says: there is usually no need to create a user crontab for root. Is that true? I mean the standard way is to edit the /etc/crontab, am I right? – peizhao May 03 '14 at 22:32
  • 4
    There is no "standard" way, thus you can have both files. I usually edit root's crontab and avoit /etc/crontab just becaused I am used to crontab -e – ek9 May 03 '14 at 22:35
  • 1
    This is useful information but it doesn't answer the actual question: should you use system crontab or root crontab? – Bobby Jack Jun 22 '21 at 10:10
11

/etc/cron.d (and its siblings cron.daily/weekly/monthly) is preferred for all system crontabs. You shouldn't need to touch /etc/crontab.

It's essential to separate cron entries in multiple files based on their functionality if you are planning to manage or automate things. Files under /etc/cron.d can be easily managed by packages or configuration management tools like puppet and chef. Root's crontab OTOH is practically un-maintainable by anything other than humans.

So in short, for system stuff, you can use /etc/cron.*. If there's something you would like the root user to do, then use root's crontab. /etc/crontab should be left untouched and managed by a package.

Sildoreth
  • 1,884
V13
  • 4,749
  • 2
    You don't explain why root's crontab should be preferred to /etc/crontab. They're both single files so maintenance issues should be the same for each. It's also worth noting that scheduling can be much more granular in a crontab file than in the cron.* directories. – Calum Halpin Aug 05 '20 at 12:58
  • 2
    @CalumHalpin I think that's covered by fleet automation like puppet or chef being better able to manage independent files than lines within a file. Also /etc/cron.d/job-name files DO let you specify a just as granular schedule for the job; along with setting variables like which SHELL to use, which USER / LOGNAME, which MAILTO address, which HOME, which PATH etc etc. It ALSO depends on your distro's cron. BSD derived, vixiecron… etc. Which is why there's a manual for the details. – dlamblin Aug 20 '21 at 00:46