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 GoogleCloudCoreExceptionGoogleException: 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): GoogleCloudBigQueryBigQueryClient->getKeyFile(Array)
#1 /home/domain/public_html/apps/vendor/google/cloud-bigquery/src/BigQueryClient.php(144): GoogleCloudBigQueryBigQueryClient->configureAuthentication(Array)
#2 /home/domain/public_html/apps/app.php(16): GoogleCloudBigQueryBigQueryClient->__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.
2
Answers
"… 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:
Based off what I read, it looks like the issue might be related to the working directory when the cron job was executed. When the cron job runs, it might not be in the same directory as your PHO script and the bigquery-keyfile.json file. You can change the working directory in your cron command, if you need an example of the code I can send it to you, Alternatively, you could also modify your PHP script ("app.php") to use an absolute path for bigquery-keyfile.json file. You can use the ‘DIR‘ constant to get the absolute path of the directory containing your current script. Let me know if this helps or have any errors. I’ll stick around!