I want to run a PHP script from cron. I setup cron with:

*/1 * * * * php  /var/www/html/cron/cron_job.php

And I can see it in crontab -u root -l, but the job is not executed. It also doesn't work when I run the command directly from the command line like this:

<?php  /var/www/html/cron/cron_job.php

I tried many answers related to cron but nothing worked. For example:

/usr/local/bin /var/www/html/cron/cron_job.php
/usr/local/bin/php /var/www/html/cron/cron_job.php
sudo php -f /var/www/html/cron/cron_job.php

The testing php script (cron_job.php) is:

php file_put_contents  ('test.txt',"test content\n",FILE_APPEND);  

File permissions are all OK and it is working when run in the browser or when opening a terminal in /var/www/html/cron/ and running php cron_job.php.

As requested in the comments, if I append 2> /tmp/php.log to php /var/www/html/cron/cron_job.php use a script with a syntax error and try to run (php /var/www/html/cron/cron_job.php 2> /tmp/php.log) from menu terminal, then the error log gets the error message.

  • 1
    You say that the command doesn't work when you run it from a terminal. Then why do you expect it to work if you run from cron? Please edit your question and clarify. Assuming that's just a typo and it does work from the terminal, we need to know what the script does. Can you show us the script? Even better, can you show us a minimal example script that reproduces the problem? Finally, add 2> /tmp/php.log to the end of your cron line and tell us i) if a file called /tmp/php.log is created and ii) if there is anything in that file.
    – terdon
    Jan 25, 2017 at 9:37
  • Does the user "php" exist? Jan 25, 2017 at 9:39
  • @RajeshKeladimath if I am not mistaken php is not user. it is programming name. I refered so many like this... askubuntu.com/questions/430509/…
    – Naga
    Jan 25, 2017 at 9:44
  • @terdon You say that the command doesn't work when you run it from a terminal. - I open terminal from start menu and try run php /var/www/html/cron/cron_job.php which is not working. But I open terminal inside /var/www/html/cron/ and try run php cron_job.php is working. I append 2> /tmp/php.log with php /var/www/html/cron/cron_job.php is php.log is empty :(
    – Naga
    Jan 25, 2017 at 9:47
  • @RajeshKeladimath this isn't /etc/crontab where there is a specific user field. This is a normal, user lever cron (just happens to be the root user's) so there is no username column.
    – terdon
    Jan 25, 2017 at 9:50

2 Answers 2


Your script is almost certainly running and creating your file. It's just not creating it where you expect it to be. You are not using any path in your script, you just use a file name. This means that the file will be created in the directory the script is running in.

That's why, when you move into a specific directory and run the script there, you think it works. Because the file is created in the directory you ran it in and so you can see it. By default, cron runs in the home directory of the user running it. Since this is root's crontab (which, by the way, is a bad idea), the file will be created in /root. So, go check:

sudo ls /root/test.txt

So, next time, give your script a full path instead of just a file name:

<?php file_put_contents  ('/path/to/test.txt',"test content\n",FILE_APPEND); ?>

Now, the file will be created in /path/to.


By default Cron jobs are executed in the user's home directory. While in your script is not provided path where the output file to be saved, it will be saved into the directory where the script is executed.

In your PHP scrip the path to the file test.txt is missing. You can set it as absolute value as it is described in @terdon's answer, or you can provide it as relative path (with proper origin). For example, if test.txt shuld be located in the same directory as cron_job.php, the content of your script should become something like as this:

    $path = realpath(dirname(__FILE__)); 
    file_put_contents  ("$path/" . 'test.txt',"test content\n",FILE_APPEND);

Alternatively, if you don't want to change the script you could change the Cron job in this way:

*/1 * * * * cd /var/www/html/cron/ && php cron_job.php

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.