Cron job as root need to sudo to another user
If the script is running in root's crontab, you should remove 'sudo' from the command. I've seen this before, where trying to 'sudo' when you're already root makes the command fail.
If you want to switch user, then the command when run as root, is 'su -l jira', not sudo -u jira.
Related videos on Youtube
Admin
Updated on September 18, 2022Comments
-
Admin over 1 year
RHEL6 In root's crontab a have:
* 21 * * * /root/script.sh
In script.sh there is a line:
/usr/bin/sudo -u jira bash -c 'cd /opt/jira/bin; ./stop-jira.sh' && echo "Jira Shut Down" >> /root/debuglog
The command executes in a regular shell but not when run from cron.
The cron process works fine and the rest of the script executes correctly, it's just the sudo part that doesn't work.
I have updated the script: swapping out sudo for su and tested in another script and it seems to work. I'll test on the actual script tonight and update this post then.
The
sudo
tosu
change corrected the issue.Thanks