My test.php
file looks like:
<?php
echo $_SERVER["myEnvVariable"];
echo getenv("myEnvVariable");
The above will return my set environment variable twice. It works when the script is called from outside the server.
If I call the same script on the server using the command line command:
php test.php
or using a cronjob
****** curl http://localhost/test.php
nothing is returned.
How do I make available my environment variables within the server itself? I am setting my environment variables within the Apache httpd.conf
file.
Execute the cron job this way:
myEnvVariable=value php test.php
Variable will be available from getenv("myEnvVariable")
, not in $_SERVER
.
If you are having lots of environment variables try this,
Export env entries to a file
env >> /etc/environment
Add the CRON job with crontab -e
5,35 * * * * . /etc/environment; /usr/local/bin/php /srv/app/artisan schedule:run >> /dev/null 2>&1
CRON: Running php scheduler at 05, 35 of each hour with source of all the environment variables. (replace with your command)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With