Ubuntu – How to run SUDO… commands in cronjobs in Ubuntu 16.04

bashcommand linecronpermissionsscripts

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?

Best Answer

Simply run crontab -e as root user . This would run your command with root permission and there is no need to add sudo before it .

However you can't login with root user and you need to perform your job as cron job you should specified full path in cron file :

 $ * * * * * /usr/bin/sudo /your/command

Also you can add NOPASSWD in front of your command in /etc/sudoers file to run command as root but without password .

Related Question