There are certain terminal commands which can only be run as a root user. If we try to execute them without sudo
keyword, they fail with the Permission denied
error. An example would be a command to make a directory/folder in certain locations.
I need to run one such command as a part of a cron job in my Ubuntu 16.04. Normally I could run the command with the sudo
keyword in the terminal and I would be prompted to enter the root user's password and then the command would be executed successfully.
But now that I have to enter this command as a part of a cron job in my crontab
file, how do I do this? How do I run such a command as a part of a cronjob?
cron
runs with root. You don't need to add sudo.sudo crontab -e
rather than plaincrontab -e
) or use the system cron file/etc/crontab
directly. Is there some particular reason you need to use a user crontab?sudo crontab -e
andcrontab -e
?sudo crontab -e
inserts the command into root's crontab file/var/spool/cron/crontabs/root
, whereascrontab -e
inserts it into$USER
's crontab file/var/spool/cron/crontabs/$USER