Search code examples
phpgoogle-bigquerycroncpanel

PHP script executes fine when run manually in terminal, but errors when cron in cPanel


I am trying to run a cron job to execute a PHP script. The command for the cron I am trying to execute is /usr/local/bin/php -q /home/domain/public_html/apps/app.php >> /home/domain/public_html/logs/cron.log. When running the job, I get the following error:

Fatal error: Uncaught Google\Cloud\Core\Exception\GoogleException: Given keyfile path bigquery-keyfile.json does not exist in /home/domain/public_html/apps/vendor/google/cloud-core/src/ClientTrait.php:130
Stack trace:
#0 /home/domain/public_html/apps/vendor/google/cloud-core/src/ClientTrait.php(96): Google\Cloud\BigQuery\BigQueryClient->getKeyFile(Array)
#1 /home/domain/public_html/apps/vendor/google/cloud-bigquery/src/BigQueryClient.php(144): Google\Cloud\BigQuery\BigQueryClient->configureAuthentication(Array)
#2 /home/domain/public_html/apps/app.php(16): Google\Cloud\BigQuery\BigQueryClient->__construct(Array)
#3 {main}
  thrown in /home/domain/public_html/apps/vendor/google/cloud-core/src/ClientTrait.php on line 130

I did notice that I would get the same error if I manually entered the cron command in terminal, but if I were to change directory to /home/domain/public_html/apps/ and run php app.php >> /home/domain/public_html/logs/cron.log or even /usr/local/bin/php app.php >> /home/domain/public_html/logs/cron.log, the script would run without any issue.

The bigquery-keyfile.json file is in the same directory as app.php. Don't know what I am missing. I've been searching everywhere and continue still. Any advice is greatly appreciated. Thank you in advance.


Solution

  • "... if I were to change directory ... the script would run without any issue" - you've answered your own question. The code you're running apparently references bigquery-keyfile.json with a relative path, ie it assumes it is running in the application directory. That's not unusual.

    You could look into updating the code and try to refer to the file with a fully qualified path, though there is the risk that something else is also expecting to be running in that directory. IMO a better solution is to just run it from the directory it expects to be running in. Update your cron command to something like:

    cd /home/domain/public_html/apps && /usr/local/bin/php -q app.php >> ./logs/cron.log