My server is running on Ubuntu 14.04 and nginx. My problem concerns Cron, I'd like to start it (crontab) using a file - cron.txt, its contents:

* * * * * /usr/bin/php /var/www/html/test.php >> /var/log/cron.log

If I run it using the command: crontab /var/www/html/cron.txt, it works perfectly, script test.php is being executed. But, when I do the same using a PHP script (executing via browser): exec('crontab /var/www/html/cron.txt');, it doesn't work.

Checking current cronjob by command: crontab -l -u www-data, the line from the file appears, but Cron does not do its job. Maybe it's related to some permission issues? Because it only works by executing as a root. Is there a way to force Cron to do www-data's tasks as well?

4 Answers 4


I do not clearly understand what are you trying to achieve. As far as I understand you need to execute a cron job as www-data user. Usually (is highly recommended) www-data user does not have access to shell. But you can execute cron tasks as www-data, placing your tasks in the root cron as follows:

* * * * * su www-data -s /bin/bash -c "/usr/bin/php /YOUR_PATH/task.php"

This is also a clean way, as you have all the cron jobs of the system centralized in one file, but each tasks runs as the needed user (with the proper permissions and attributes).

You can change the * * * * parameters in order to adjust the schedule of your job. In my example it runs every one minute.

Hope it helps.


You want something strange. Why you execute crontab from php script? Crontab is a time-based job scheduler, it shouldn't run from php script. If you want start cronjob as www-data, you should configure it by command crontab -u www-data -e and after this your job would be executed as you configured it.

  • I want a php script only to start crontab when it's necessary.
    – BociucH
    Jun 6, 2017 at 20:15
  • @BociucH you don't need to start crontab, you could run script. Jun 6, 2017 at 20:16
  • It's all about sending emails, because I want to send like 50 emails every 10 minutes. When a user starts the mailing, a php script starts cron and when sending emails is done, a php script stops cron. That's my idea for this.
    – BociucH
    Jun 6, 2017 at 20:19
  • 5
    That's not what cron is for. Suggest you close this question and ask a question at a high level - ie what you want to achieve, rather than asking us low level questions. You can cut down a tree with a spoon, but it's much easier if you use the correct tool.
    – Tim
    Jun 6, 2017 at 20:23
  • 3
    @BociucH it strange. You could configure your crontab like this */10 * * * * and make limit for mail sending for 50 e-mails. Cron shouldn't be executed inside script, it's wrong way. Jun 6, 2017 at 20:23

I managed to achieve what I wanted, everything about the code in my initial post is ok, the only thing I should've known is that the www-data user is not allowed to write in /var/log, so I had no output there. And in addition, in my PHP script I was trying to create a file and write something into it, to do is, the absolute path is required, so it should look like:

dirname(__FILE__) . '/myfile.txt';

After these changes, everything works as expected.


Nextcloud needs the same thing. They don't seem able to get PHP to set up a Cron job in their installer and insist that the Linux admin do it.

So, for your first Q: How do I use PHP to set up a www-data crontab job? A: You don't, that needs to be done from Shell.

As for your second Q: Can crontab run a www-data job? A: Totally, but that part is done at the time the crontab job is set up. Using www-data to execute a cron job from inside any cron file is a bad idea, just let www-data run the crontab job itself.

Here's how I made it work:

  1. Your string should probably be:

* * * * *php -f/var/www/html/test.php >> /var/log/cron.log

  1. www-data is finicky as a Cron user.

From the terminal, set up Cron this way:

Running as root...

crontab -u www-data -e ...add the string to run, similar to in your Q.

...But, you seem to want to use PHP to set up the crontab job. (I assume this is a PHP installer.) Having a PHP app set up a crontab job might not be possible. It might not even be a good idea. If you can do it, be sure to let Nextcloud devs know because they don't seem to know how either.

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.