0

I have the following problem. I want to run a cronjob once a day within a Docker container

@daily /usr/local/bin/php /var/www/html/Tests/example.php

This script sends requests to ChatGPT. The corresponding OpenAIKey is stored in docker-compose.yaml as an enviroment variable. The OpenAIKey was previously hardcoded but has now been moved to an .env file (contains only OpenAIKey). When the key was still hardcoded, the cronjob still worked fine and manual execution of the example.php is also possible now. The .env was therefore correctly integrated into the existing code. However, as soon as the cronjob is supposed to execute the example.php (with .env-file) the following error appears within the Docker container:

"error": { "message": "You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you're accessing the API from your browser and are prompted for a username and password. You can obtain an API key from https://platform.openai.com/account/api-keys.", "type": "invalid_request_error", "param": null, "code": null } }

Unfortunately I do not know how to fix this error, it was not possible via a bash-script. Adding the key as source in the crontab did not work either ->

@daily source ../openAIKey.env && /usr/local/bin/php /var/www/html/Tests/example.php 2> /var/www/html/Tests/errPress.txt >> /var/www/html/Tests/errPress.txt
7
  • More details of how the .env is constructed, what is the content (not the actual key) and is in the PHP script? Commented Jan 28 at 20:32
  • The .env only contains the key (openAIKey = ...). Within the script, the key is returned in a static function (return getenv(“OPENAI_API_KEY”);). and the example.php is the script that the cronjob should execute, which also needs the Key Commented Jan 28 at 20:42
  • Several things to look into. Sourcing the file may not necesarrily mean it exports the variable as environment from the operating system (getenv says it takes it either from putenv or from os). Keys may mismatch? And the other things is whether a subshell is created when you invoke php yourscript and the result of the sourcing to be discarded in previous shell Commented Jan 28 at 20:59
  • Can I check somehow if a subshell is created when I invoke my script? I actually heard of this for the first time right now. And if a subshell is created is there any possibility that the sourcing is not discarded? Commented Jan 28 at 21:26
  • Probably you can print in the script all variables like print_r(getenv()) to see the assoc array after the execution. Honestly, && should not create a subshell, but worths checking what you have in the env array Commented Jan 28 at 21:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.