简体   繁体   English

使用shell_exec()运行shell脚本时,由putenv()设置的环境变量不存在。 的PHP

[英]Environment variable set by putenv() are not present while running a shell script using shell_exec() | PHP

I have a PHP script in which I am setting a environment variable using putenv() and then I am calling a shell script using shell_exec() in this PHP script. 我有一个PHP脚本,在其中使用putenv()设置环境变量,然后在此PHP脚本中使用shell_exec()调用Shell脚本。 In shell script I am trying to use that environment variable. 在shell脚本中,我尝试使用该环境变量。

My PHP script is like: 我的PHP脚本是这样的:

<?php
...
...
putenv("USERHOME=/home/user/mnt");
...
...
shell_exec("sudo ./script.sh arg1 arg2");
...
...
?>

My shell script is like: 我的shell脚本是这样的:

#!/bin/sh
...
...
echo "USER HOME=" $USERHOME
...
<---Another use of $USERHOME----->
...
exit $?

I am using Xampp for my application and PHP version is 5.3.8. 我正在为我的应用程序使用Xampp,PHP版本是5.3.8。

Previously it was working fine on fedora core 3. But recently I moved my application to CentOS 7 and now it is not working. 以前,它在fedora core 3上运行良好。但是最近,我将应用程序移至CentOS 7,但现在不起作用。 On CentOS 7, I am getting NULL value of USERHOME variable. 在CentOS 7上,我得到USERHOME变量的NULL值。

In PHP < 5.4 there's a safe_mode directive that limits what environment variables you can write with putenv . 在PHP <5.4中,有一个safe_mode指令来限制可以使用putenv编写的环境变量。 Edit your php.ini file and remove USERHOME from the safe_mode_protected_env_vars setting. 编辑您的php.ini文件, USERHOMEsafe_mode_protected_env_vars设置中删除USERHOME

http://php.net/manual/en/ini.sect.safe-mode.php#ini.safe-mode-protected-env-vars http://php.net/manual/zh/ini.sect.safe-mode.php#ini.safe-mode-protected-env-vars

After some research and debugging, I am able to get the correct value of environment variable in my shell script, which is set by putenv() in my PHP script. 经过一些研究和调试,我能够在我的shell脚本中获得正确的环境变量值,该值由我的PHP脚本中的putenv()设置。

First of all, I am new to PHP and here also. 首先,我是PHP的新手,这里也是。 And I missed some things in question while posting it. 我在发布时错过了一些有问题的东西。 Now I have edited my question and provided all the required details and script contents correctly. 现在,我已经编辑了问题,并正确提供了所有必需的详细信息和脚本内容。

Root Cause and Resolution: 根本原因和解决方法:

In my PHP script I was running the shell script through shell_exec() and was using sudo in the command shell_exec("sudo ./script.sh arg1 arg2"); 在我的PHP脚本中,我通过shell_exec()运行shell脚本,并在命令shell_exec("sudo ./script.sh arg1 arg2");使用sudo shell_exec("sudo ./script.sh arg1 arg2"); This sudo is the main problem here. 这个sudo是这里的主要问题。

Removing it and calling the script simply shell_exec("./script.sh arg1 arg2"); 删除它并简单地调用脚本shell_exec("./script.sh arg1 arg2"); did the trick. 做到了。

The shell_exec() call with sudo was working fine on Fedora Core 3. But when I moved my application to CentOS 7, it stopped working. 使用sudo的shell_exec()调用在Fedora Core 3上运行良好。但是,当我将应用程序移至CentOS 7时,它停止了工作。 After removing sudo from the command it is running as expected on CentOS 7. 从命令中删除sudo后,它会在CentOS 7上按预期运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM