30

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.

  • 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). – Skippy le Grand Gourou Jan 18 '17 at 11:41
38

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
  • 1
    actually second option doesn't work on most versions of Ubuntu – user123 Apr 1 '15 at 6:47
  • The second option does work for me on 12.04LTS. Maybe it doen't on later versions? – mivk May 10 '15 at 13:31
  • 1
    second option is not working on 14.04 – dgoosens Jul 28 '15 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 '17 at 10:10
  • 2
    For second option try this instead sudo su -c "crontab -e" www-data -s /bin/bash – Lunfel Jun 13 '18 at 4:15
47

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

sudo crontab -u www-data -e
  • this works on 14.04 – dgoosens Jul 28 '15 at 21:41
3

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
  • This one worked for me on Ubuntu – tristanbailey Feb 5 '17 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. – HörmannHH Feb 6 at 19:28
  • @RichieHH, Why should i add the sudo? Who said the user is not already running as root ? Anyone who uses this command should have enough knowledge to know that if he needs to run the command with privileges will have to run it with sudo, otherwise better don't touch.. and you say more concisely ... ? code commander just typed the command but didnt give any more information – spacebiker Feb 7 at 11:01
  • .. if you are that smart, you better start giving answers instead of criticizing the work of others, maybe then you start getting some reputation – spacebiker Feb 7 at 11:12
-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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.