0

I have setup a crontab job to run every hour.

m | h | d | M | w | command
--|---|---|---|---|----------
0 | * | * | * | * | php path/job.php

Inside job.php I have this code:

<?php
    $today = date('Y-m-d');
    echo   "echo: Today is $today <br>";
    printf("printf: Today is $today \n");

    file_put_contents("/path/$today.log","log file created");
    exit();

When I visit job.php on my browser, I see the expected output:

echo: Today is 20-08-2018
printf: Today is 20-08-2018

And a new file 20-08-2018.log is created.

However, when the crontab runs this job.php, I get an email notification of the output generated by the job, and it only contains:

printf: Today is 20-08-2018

Moreover, I check if the file is generated/appended, but fail to find any evidence of the file getting generated (even if I delete all log files before waiting for the crontab to run the job).

How can this be explained? What can I do to make file_put_contents work when a crontab job is automatically triggered?

Edit: I forgot to mention that I have checked for php_errorlog suspecting something went wrong when crontab triggers the job, but failed to find any error.

Ahmad
  • 12,336
  • 6
  • 48
  • 88
  • 2
    I can't explain absence of `echo`. For `file_put_contents`, make sure to check which directory you're in. – Amadan Aug 21 '18 at 04:15
  • How is the email sent? Could you write the output of the CRON to a file with `>` or `>>`? I had a similiar issue with `/tmp/` on AWS had to use `find . -name` to figure out there was an alias for the real `tmp` dir. – user3783243 Aug 21 '18 at 04:18
  • @user3783243 i get an email from cPanel as a service in my website hosting provider – Ahmad Aug 21 '18 at 04:19
  • Have you set it up to report any errors? See [How to get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) – Phil Aug 21 '18 at 04:20
  • I would try having the CRON write what it actually has. – user3783243 Aug 21 '18 at 04:20
  • @Amadan I did check the path, no file is created if the job is run through crontab. Only when the browser accesses the page – Ahmad Aug 21 '18 at 04:20
  • 1
    How **exactly** did you _"check the path"_? Did you use `realpath()` I suggest you use `file_put_contents(__DIR__ . "/$today.log", ...)` to write to a file in the same directory. Also, what user owns that particular crontab? It's always a good idea to use full paths to binaries in crontab too, eg `/usr/bin/php` as it may not have the same `PATH` as you expect – Phil Aug 21 '18 at 04:36

2 Answers2

2

@Ahmad: Try this Solution

Try adding full path in file_put_contents() function and giving appropriate folder permission.

ex: file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . "{$today}.log", "log file created")

  • This did really help. I can now see the files getting generated with the timestamp. – Ahmad Aug 21 '18 at 04:57
  • I feel inclined to mark this as the answer, but I still do not know why printf works but echo does not. – Ahmad Aug 21 '18 at 04:58
  • 1
    Try this : Will update or generate the file 0 * * * * php path/job.php > full/path/output.txt or to append to same file 0 * * * * php path/job.php >> full/path/output.txt – Manjunath Muniraju Aug 21 '18 at 06:17
0

Try This

$today = date('Y-m-d');
$myfile = fopen("/path/$today.log", "a") or die("Unable to open file!");
$txt = "Today is $today <br> \n";
echo   "echo: Today is $today <br> \n";
fwrite($myfile, $txt);
fclose($myfile);
exit();
LahiruTM
  • 638
  • 4
  • 16
  • 1
    Why would this make any difference? `file_put_contents()` is really just a convenience function for the same operations as in your answer – Phil Aug 21 '18 at 04:40