duanlei4759 2015-10-29 03:13
浏览 113

全局环境变量不适用于在PHP中执行的bash命令

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?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何实验stm32主通道和互补通道独立输出
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题