0

I need to update the crontab of my web user (the user that is executing all websites). In my crontab I'm using shell script with commands that needs to use "sudo".

The script is well executed but I get error messages, a few examples below :

mv: cannot move 'cms/configs/database.json' to './database.json': Permission denied
mv: cannot move 'cms/configs/options.json' to './options.json': Permission denied
mv: cannot move 'cms/configs/analytics.json' to './analytics.json': Permission denied
mv: cannot move 'cms/sql/patchs.json' to './patchs.json': Permission denied
mv: cannot move 'cms/public/uploads' to './uploads': Permission denied
mv: cannot move 'cms/node_modules' to './node_modules': Permission denied

And I put the crontab declaration using : sudo crontab -u mywebuser -e

If I execute the script without crontab using "sudo myscript.sh" everythings works as expected.

tonymx227
  • 101

2 Answers2

0

Install a system job file that is run by that user. /etc/cron.d/web for example, rather than the user via the crontab command.

5 4 * * * mywebuser /opt/thing/myscript.sh

Assuming your crond supports this, Vixie does.

An advantage is crond is now responsible for becoming the correct user, rather than you. In other words, you don't need to define the configuration and environment to make sudo work.

Replace with your desired time, user, and script. And be sure the files have the correct permissions to be managed by this user.

John Mahowald
  • 32,050
  • 2
  • 19
  • 34
0
sudo myscript.sh

(Without an alternative user defined with sudo -u username ) will default to running the script as root.

sudo crontab -u mywebuser -e

On the other hand will set up a cron job to run, not as root, but with the much more limited permissions of user “mywebuser” ...

That user will not be able to modify files owned by other users such as for instance the root user....

Fix your file and directory ownership and permissions

Bob
  • 5,805
  • 7
  • 25