When to Use Sudo with Crontab

crontab -e edits the crontab for the current user, so any commands contained within will be ran as the user who's crontab you are editing. sudo crontab -e will edit the root users crontab, and so the commands within will be run as root.


To add on to cduffin, use the minimum permissions rule when running your cronjob. If the job can effectively be run as a regular user, have the job run as them. If the cronjob requires escalated privileges, add the job as the root user.

To be super security conscious, you can figure out what root level commands need run and set the regular user up in the /etc/sudoers file to be allowed to run that single command... Example:


Defaults:johndoe !requiretty
johndoe ALL=NOPASSWD:/usr/bin/tail /var/log/yum.log*

Tags:

Cron