Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. We don’t allow questions about general computing hardware and software on Stack Overflow. You can edit the question so it’s on-topic for Stack Overflow. Closed 2 years ago. Improve this question
I have few crontab jobs that run under root, but that gives me some problems. For example all folders created in process of that cron job are under user root and group root. How can i make it to run under user www-data and group www-data so when i run scripts from my website i can manipulate those folders and files?
My server runs on Ubuntu. Current crontab job is:
*/1 * * * * php5 /var/www/web/includes/crontab/queue_process.php >> /var/www/web/includes/crontab/queue.log 2>&1
Instead of creating a crontab to run as the root user, create a crontab for the user that you want to run the script. In your case, crontab -u www-data -e
will edit the crontab for the www-data user. Just put your full command in there and remove it from the root user's crontab.
EDIT: Note that this method won't work with crontab -e, but only works if you edit /etc/crontab directly. Otherwise, you may get an error like /bin/sh: www-data: command not found
Just before the program name:
*/1 * * * * www-data php5 /var/www/web/includes/crontab/queue_process.php >> /var/www/web/includes/crontab/queue.log 2>&1
crontab -e
, but only works if you edit /etc/crontab
directly. Read the comment at the top of this file for more information.
sudo crontab -e
, I see the jobs. But I open the file /etc/crontab
there are no jobs. Is this weird? Also if I sudo crontab -e
and add a job with user specified, will it work?
Since you're running Ubuntu, your system crontab is located at /etc/crontab
.
As the root user (or using sudo), you can simply edit this file and specify the user that should run this command. Here is the format of entries in the system crontab and how you should enter your command:
# m h dom mon dow user command
*/1 * * * * www-data php5 /var/www/web/includes/crontab/queue_process.php >> /var/www/web/includes/crontab/queue.log 2>&1
Of course the permissions for your php script and your log file should be set so that the www-data
user has access to them.
crontab -e
though; but not.
Mike's suggestion sounds like the "right way". I came across this thread wanting to specify the user to run vncserver
under on reboot and wanted to keep all my cron jobs in one place.
I was getting the following error for the VNC cron:
vncserver: The USER environment variable is not set. E.g.:
In my case, I was able to use sudo
to specify who to run the task as.
@reboot sudo -u [someone] vncserver ...
sudo: sorry, you must have a tty to run sudo
You can also try using runuser
(as root) to run a command as a different user
*/1 * * * * runuser php5 \
--command="/var/www/web/includes/crontab/queue_process.php \
>> /var/www/web/includes/crontab/queue.log 2>&1"
See also: man runuser
runuser
is not included in Ubuntu.
Success story sharing
crontab -e
as a specific user.cron
on *nix systems doesn't require a user to login in order to run the jobs specified in a specific user's crontab.