44

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.

Thanks.

1
  • Note that some crontab scripts which run as www-data (e.g. for awstats, or php5 sessionclean) are located in /etc/cron.* directories and thus not visible through crontab -e (even when specifying user www-data). Jan 18, 2017 at 11:41

5 Answers 5

76

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

sudo crontab -u www-data -e
2
  • this works on 14.04
    – dgoosens
    Jul 28, 2015 at 21:41
  • This is exactly what we needed. CODE COMMANDER! lol love that name, Ubu 18.04
    – dustbuster
    Oct 18, 2023 at 15:28
54

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 -s /bin/bash
7
  • 2
    actually second option doesn't work on most versions of Ubuntu
    – Ruslan
    Apr 1, 2015 at 6:47
  • The second option does work for me on 12.04LTS. Maybe it doen't on later versions?
    – mivk
    May 10, 2015 at 13:31
  • 1
    second option is not working on 14.04
    – dgoosens
    Jul 28, 2015 at 21:41
  • For ease of management, the line can also be put in its own file in /etc/cron.d with a descriptive name, e.g., /etc/cron.d/artisan.
    – fkraiem
    Dec 15, 2017 at 10:10
  • 3
    For second option try this instead sudo su -c "crontab -e" www-data -s /bin/bash
    – Lunfel
    Jun 13, 2018 at 4:15
9

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
2
  • This one worked for me on Ubuntu Feb 5, 2017 at 7:52
  • This was previously answered correctly and more concisely by code commander above 4 years previously. You omitted the use of "sudo". and threw in unrequested information regarding the format of the cron line.
    – RichieHH
    Feb 6, 2019 at 19:28
1

I would like to add another approach. As other people mentioned, Ubuntu (16.04 here) and www-data crontab seems to be unreliable (maybe it's a security thing?).

Anyway, in our company we like to have all cronjobs on a server easily accessible, so you don't miss anything. At the same time we don't want to run everything (anything really!) as root.

Therefore we run

sudo crontab -e 

As you normally do, and then we specify the command as

* * * * * /bin/su - www-data -s /bin/bash -c '/path/to/command'

This will execute /path/to/command as www-data while keeping the cronjob in root cronjobs file (and that will always run correctly). It has the nice benefit of being able to write the logfiles as root (for max security) using pipes.

Note that we are passing our prefered shell, this could also be /bin/sh for a simpler shell (we just like full bash capabilities). Www-data doesn't have a shell specified so you will get errors without it. Normally cron runs jobs with /bin/sh only.

0

Important: www-data user doesn't have permission to execute.

So: you need to su and add -s to run the command as www-data user with the shell.

Example for Laravel

    * * * * * su www-data -s /bin/bash -c "/path_your_project/artisan schedule:run"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .