How to specify in crontab by what user to run script?
Solution 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.
Solution 2
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
Solution 3
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.
Solution 4
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 ...
Solution 5
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
Related videos on Youtube
Comments
-
arma almost 2 years
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
-
arma over 12 yearsThat would make it run as apache user witch is www-data right?
-
imgx64 almost 11 yearsNote that this method won't work with
crontab -e
, but only works if you edit/etc/crontab
directly. Read the comment at the top of this file for more information. -
James almost 11 yearsThe user can only be specified in the system crontab
-
zinking about 10 yearsI thought this should be equivalent to
crontab -e
though; but not. -
kulak about 10 yearsIt works the same way when you use
crontab -e
as a specific user. -
Hemm over 9 yearsThe Ubuntu docs have recommended not editing /etc/crontab as it can be overwritten by updates. crontab -e will create a user-specific cron file in /var/spool/cron/crontabs.
-
Bruno Finger about 9 yearsBut will this crontab be checked at system start-up or only when the user logs in?
-
Mike about 9 years
cron
on *nix systems doesn't require a user to login in order to run the jobs specified in a specific user's crontab. -
askyle about 9 yearsWill this also set the group id as the OP asked? What if the group one wants is different from the user's primary group?
-
Renato Gama almost 9 yearsI got this message on the logs:
sudo: sorry, you must have a tty to run sudo
-
kaiser over 8 years@renatoargh You are probably running on RedHat. Take a look at this answer on Unix.SE.
-
enemetch almost 8 yearsI have a few jobs on schedule on system crontab. When I do
sudo crontab -e
, I see the jobs. But I open the file/etc/crontab
there are no jobs. Is this weird? Also if Isudo crontab -e
and add a job with user specified, will it work? -
Raptor almost 8 years
runuser
is not included in Ubuntu. -
Russell E Glaue almost 8 yearsrunuser is in the latest version of Ubuntu. There are also alternatives to runuser as discussed in this answer unix.stackexchange.com/questions/169441/ubuntu-runuser-command , e.g. su, and sudo.