Ubuntu: How to run crontab as user:www-data?


My LAMP is setup to work as user:www-data and all files and folders are created with that permissions.

I have setup for crontab as user@ubuntu.

So i do crontab -e and use this command:

*/5 * * * * php /var/www/public/voto_m/artisan top >/dev/null 2>&1  

Basically that command just creates cache file in specified place (no problems with that), but that cache file is created with user:user permissions not user:www-data permissions.

How can i make that it will by default create file with user:www-data permissions?
I can't go and chown each time file is recreated.



You can write your entry into the system crontab /etc/crontab, which takes an additional argument specifying the user to run as (usually root, but can be www-data).

Your line would become:

*/5 * * * * www-data php /var/www/public/voto_m/artisan top >/dev/null 2>&1  

Or you can edit the crontab of user www-data with su:

sudo su -c "crontab -e" www-data  


You can also run crontab with the -u argument to edit a crontab for a specific user:

sudo crontab -u www-data -e  


To run a crontab as user www-data you can use the following command:

crontab -u www-data -e  

Then you write a line, for example to run a php file every 15 minutes:

*/15  *  *  *  * php -f /path_to_cron/cron.php  

When saving it, you will be asked by the editor:

File Name to Write: /tmp/crontab.HMpG7V   

Save it there, no worries. crontab -e opens a file in /tmp instead of the actual crontab so that it can check your new crontab for errors and prevent you from overwriting your actual crontab with those errors. If there are no errors, then your actual crontab will be updated. If crontab -e just wrote straight to your actual crontab, then you would risk blowing away your entire crontab.

To verify that your cronjob runs, you can check the cron logs. typically in /var/log/cron.log or executing the following command:

crontab -u www-data -l  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »