'shell_exec doesn't load environmental variables
I have a function on admin panel that allows admins run CronJobs. I've implemented it through shell_exec function. There are two problems.
Problem 1
Script that executes shell_exec runs with user rc which I test with echo get_current_user(); while the command executed with shell_exec is ran with www-data which I test by running var_dump(shell_exec('whoami')); and var_dump(shell_exec('php -r "echo get_current_user()"')).
Problem 2
The www-data user doesn't recognise global environmental variables. I've added APPLICATION_ENV=development to /etc/environment which works fine because when I run sudo -u www-data echo $APPLICATION_ENV it displays development. Running var_dump(shell_exec('echo $APPLICATION_ENV')) displays empty string.
Am I missing something?
Solution 1:[1]
You should be able to accomplish this with
var_dump(shell_exec('source /etc/environment && echo $APPLICATION_ENV'));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Guido Faecke |
