Should I edit /etc/crontab or run crontab -e as root?
It might be useful to note that jobs in a personal crontab (
crontab -e) are always executed as their owner, where
/etc/crontab contains an additional mandatory
<user> field allowing an admin to configure the job to run as a non-root user.
Editing the system crontab or setting up a personal crontab for root are probably a bit more portable, not specific to certain Linux distributions and arguably more convenient for a person to maintain, with all jobs in a single file but:
Personally I favour a third option: for each scheduled task drop either
- a file in
/etc/cron.d/with a cron snippet
- an executable (script) in the relevant
/etc/cron.[hourly |daily |weekly |monthly]directory.
That is easier to script (you can simply create/overwrite/delete such files and you don't have to muck about in the contents of a single crontab file) and that works well with configuration management tooling and that is what package managers are already doing anyway.
/etc/cron.[hourly |daily |weekly |monthly] are always executed as root, where the cron snippets in
/etc/cron.d/ allow both setting a custom schedule as well as running as a different user with that same mandatory
<user> field found in
As best I recall,
crontab -e has the added advantage that it verifies the crontab syntax before installing it, and will error and restore the previous if you make a mistake. This way, anything that was previously working won't suddenly stop if you get the syntax wrong. I think best practise is to use the utilities, like running
visudo rather than editing
It is really a style question, there is a reason multiple methods are offered by the OS. Just be consistent and do not mix and match if you do not want to confuse anyone else (or yourself after some time of not dealing with the system) - if it is hard to see what tasks are actually scheduled across the whole host, it tends to end in nasty surprises.
In order to be sure for adding a cron job which require a specific user 's rights, i personally use the following command :
# crontab -u <user> -e
You can add
As @rackandboneman stated, there is no need to mess with /etc/cron.d/ files. If the matter is about user 's cron jobs, use the features of