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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 matlab解优化问题代码
    • ¥15 写论文,需要数据支撑
    • ¥15 identifier of an instance of 类 was altered from xx to xx错误
    • ¥100 反编译微信小游戏求指导
    • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
    • ¥15 学不会递归,理解不了汉诺塔参数变化
    • ¥15 基于图神经网络的COVID-19药物筛选研究
    • ¥30 软件自定义无线电该怎样使用
    • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
    • ¥15 Jenkins+k8s部署slave节点offline