I'm trying to return the value of a global environment variable inside a bash script which is executed from inside PHP (run as sudo).
Running the command printenv
from the bash script reveals that the global variable is not set. I understand sudo resets the environment.
Please consider the following setup;
Define global environment var
root@test:/# export HEY=hello
Test existence of var
root@test:/# printenv | grep HEY
HEY=hello
Please consider this PHP function
PHP function
function testAction() {
define('TEST_CMD', '/usr/bin/sudo /usr/local/test/bin/');
$command = 'bash_script';
$query = TEST_CMD.$command;
exec ($query, $output, $return_var);
var_dump( $output );
var_dump( $return_var );
echo exec('whoami');
}
Contents of /etc/sudoers.d/admin
admin ALL=(ALL) ALL
admin ALL=NOPASSWD:/usr/local/test/bin/*
Contents of /usr/local/test/bin/bash_script
#!/bin/bash
printenv
echo $HEY
exit
I have tried putting Defaults env_keep +="HEY"
inside /etc/sudoers.d/admin
too but that doesn't appear to work either.
OS is ubuntu 14.04
Any ideas?