PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用proc_open()管道加载.profile大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

情况是这样的:我编写了一个在特定服务器上运行的后端应用程序.在此服务器上,可以通过ssh从前端服务器执行脚本.然后,我的脚本将检查是否正确加载了所需的环境变量,因为我在脚本本身中高度依赖它们.

这行得通,尽管不是我希望事情正常进行的方式.建立连接后,仅使用exec(‘source /home/user/.profile’)即可加载./profile.当然行不通.由于脚本已经在运行.
这就是为什么脚本像这样启动的原因:

#!/to/PHP/bin/PHP -n
<?PHP
    if (!$_SERVER['VAR_FROM_PROFILE'])
    {
        exec('/absolute/path/to/Helperscript '.implode(' ',$argv),$r,$s);
        if ($s !== 0)
        {
            die('Helper script fails: '.$s);
        }
        exit($r[0]);
    }

该帮助脚本是ksh脚本:

#!/path/ksh
source /.profile
$*

加载配置文件,然后再次调用一个脚本.
我希望第二个脚本消失,我觉得这很愚蠢……需要第二个脚本来运行第一个脚本.我知道可以使用proc_open来设置环境值,但是将.profile重写为数组可能会更加愚蠢.
我还尝试过proc_open一个sHell,加载配置文件并从自身内部再次运行该脚本.只是发现脚本不断调用自身,使我相信该配置文件根本没有加载.

到目前为止,这是我的尝试:

#!/to/PHP/bin/PHP -n
<?PHP
    if (!$_SERVER['VAR_FROM_PROFILE'] && $argv[1] !== 'fromself')
    {
        $res = proc_open('ksh',array(array('pipe','r'),array('pipe','w'),array('pipe','w')),$pipes);
        usleep(5);
        fwrite($pipes[0],'source /home/user/.profile & '.$argv[0].' fromself');
        fclose($pipes[0]);//tried using fflush and a second fwrite. It Failed, too
        usleep(1);
        echo stream_get_contents($pipes[1]);
        fclose($pipes[1]);
        proc_close($res);
        exit();
    }
    var_dump($_SERVER);
?>

到目前为止,我还没有运气,有人能告诉我我是否在这里忘记了什么吗?我究竟做错了什么?我在这里俯瞰什么吗?

解决方法:

我没有ksh,但我设法用bash做到了.

/home/galymzhan/.bash_profile:

export VAR_FROM_PROFILE="foobar"

/home/galymzhan/test.PHP

#!/usr/bin/PHP -n
<?PHP
if (!isset($_SERVER['VAR_FROM_PROFILE'])) {
  $descriptors = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'));
  $process = proc_open('bash', $descriptors, $pipes);
  fwrite($pipes[0], escapesHellcmd('source /home/galymzhan/.bash_profile') . "\n");
  fwrite($pipes[0], escapesHellcmd('/home/galymzhan/test.PHP') . "\n");
  fclose($pipes[0]);
  echo "Output:\n";
  echo stream_get_contents($pipes[1]);
  echo "\n";
  fclose($pipes[1]);
  proc_close($process);
  exit;
}
print "Got env var {$_SERVER['VAR_FROM_PROFILE']}\n";
// Useful part of the script begins

我得到的输出

[galymzhan@dinohost ~]$./test.PHP 
Output:
Got env var foobar

大佬总结

以上是大佬教程为你收集整理的使用proc_open()管道加载.profile全部内容,希望文章能够帮你解决使用proc_open()管道加载.profile所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: