1

I am creating the cronjob from my Plesk control panel.

The cron is like so:

0 0,3,6,9,12,15,18,21 * * * /usr/bin/php httpdocs/sources/australia.php >> /var/www/vhosts/website-here/logs/topfbk.log 2>&1 

The script runs and after 3 seconds it errors out, but it does not output anything to the log file so I am unable to know which is the cause.

This used to work on the same server using Ubuntu 18.04, I have upgraded to Ubuntu 20.04 and cannot seem to get this work.

Giacomo1968
  • 53,069
  • 19
  • 162
  • 212
  • have u tried runing directly from console? – JTE Oct 24 '20 at 21:51
  • What log file do you mean? The PHP error log? By default the PHP CLI doesn’t log errors unless you make an effort. Look at [my answer here on Stack Overflow](https://stackoverflow.com/a/24916609/117259) that explains the steps needed to enable PHP CLI error logging. – Giacomo1968 Oct 24 '20 at 22:02
  • @JTE same, I must mention that I am using php 7.4.3 cli – Cristian Badea Oct 24 '20 at 22:30
  • @Giacomo1968 I am referring to the log file that I am outputing to: /var/www/vhosts/website-here/logs/topfbk.log 2>&1 – Cristian Badea Oct 24 '20 at 22:30
  • It is a scraper that uses PDO, CURL. Checked them both, they are running on the server. – Cristian Badea Oct 24 '20 at 22:33
  • @CristianBadea That’s the Apache log file. That will only ever log issues with PHP when it is running as a module under Apache. When you run PHP from the command line, that is a completely different PHP on the system. Look at [my other answer here](https://superuser.com/a/971895/167207) that explains the confusion some people have between PHP versions in Apache versus the command like. – Giacomo1968 Oct 24 '20 at 22:36
  • I have checked, the cli and the phpinfo() shows the same version, 7.4.3 – Cristian Badea Oct 24 '20 at 22:44
  • 1
    @CristianBadea The issue is not that PHP is running the same version, but that errors from PHP in the command line will not be logged to the same place as it would with an Apache PHP script. PHP command line does not ever log errors *unless you force it to*. And your log of `/var/www/vhosts/website-here/logs/topfbk.log` will never be written to if the `httpdocs/sources/australia.php` has problems. I [posted an answer](https://superuser.com/a/1597106/167207) with a fuller explanation, but your debugging needs to go past simply checking the log you are manually creating via script output. – Giacomo1968 Oct 24 '20 at 22:47

1 Answers1

0

“The script runs and after 3 seconds it errors out, but it does not output anything to the log file so I am unable to know which is the cause.”

And in the comments you state you are checking the log in your cron job command located here:

/var/www/vhosts/website-here/logs/topfbk.log

If your command is this:

/usr/bin/php httpdocs/sources/australia.php >> /var/www/vhosts/website-here/logs/topfbk.log

Then if the script is failing it will never be able to append output to /var/www/vhosts/website-here/logs/topfbk.log.

The only way you can really debug this is to check the actual PHP command line error log; not a log you have arbitrarily created. By default the PHP command line interface does not log errors to a log file. I explain how to establish a PHP command line error log file here in this Stack Overflow post. It’s not that complicated and valuable to do in a case like this.

But past any of this, is that httpdocs/sources/australia.php the true full path to your script? When the cron job runs, are you sure it is running within a directory where that path is valid?

I would recommend running that command manually from the command line with the full path to the script like this:

/usr/bin/php /full/path/to/httpdocs/sources/australia.php

Just replace /full/path/to/ with the actual full path and see if that works. If it works, then the lack of a full path is the problem. If it doesn’t work, establishing the PHP command line error log will shed clues on what else might be happening in your code.

Giacomo1968
  • 53,069
  • 19
  • 162
  • 212
  • I have added the full path, e.g.: /usr/bin/php /full/path/to/httpdocs/sources/australia.php and it is doing the same. I will enable logging as per the steps outlined in the article and let you know the outcome. Thank you – Cristian Badea Oct 24 '20 at 22:57
  • 1
    I have followed the tutorial, but still, cannot get any output to the file. The test with php -r "error_log('This is an error test that we hope works.');" worked though, i can see it in there. – Cristian Badea Oct 24 '20 at 23:57
  • @CristianBadea Don’t give up! If the PHP CLI error log shows nothing inside it when the script runs but you can see errors when you run `error_log('This is an error test that we hope works.');` that means that for some reason the PHP script is not running. I would check permissions on the script itself and see what happens. – Giacomo1968 Oct 25 '20 at 00:17
  • The script is on 755, i switched to 777 just to check and it does the same. I am not sure why it worked before and also why is it working in localhost and not in here. It uses curl + pdo and that is about it. If needed, I can post the file on pastebin or something – Cristian Badea Oct 25 '20 at 00:27
  • @CristianBadea I don’t believe this is an issue with your PHP code at all. That said, I have helped you as much as I can. If my answer was helpful, please be sure to upvote it. And if it’s the answer that somehow solves your issue, please check it off as such. – Giacomo1968 Oct 25 '20 at 00:44