Running cron job with sudo


#1

I’ve got a cron job which needs to run with elevated permissions on a PS, and I’ve added a line (using a PS Admin User account) to the root crontab using:

The crontab is something like this:

However, the script does not seem to run, and I can’t find any trace of an error in the log, or by running: cat /var/log/syslog | grep cron

It looks like the cron isn’t being run, and I have no idea why. Can anyone see what I might be doing wrong?

(In my case, myscript.sh needs to be run using elevated permissions as it’s rebuilding a Sphinx search index. The same script runs fine outside of cron.)


#2

where’s the sudo in your crontab? using sudo to edit your crontab won’t make sh run as sudo


#3

I thought ‘sudo crontab -e’ would mean that the crontab for the root user would be edited.

I’ve since solved the problem by using sudo inside the crontab of my admin user. The crontab now looks like this:

(just the sudo added).

At first, I noticed the job failed because sudo was expecting a password. So, I edited the sudoers file to allow this script to be run without prompting for a password:

Added this line to the end of the file:

N.B. Noticed that if you add a new ‘admin user’ from the Dreamhost control panel, the sudoers file seems to be recycled and you will lose all of your changes :frowning: (something for people to be aware of!)