用proc_open()加载.profile [英] load .profile with proc_open()

查看:108
本文介绍了用proc_open()加载.profile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here's the situation: I wrote a back end application, that runs on a certain server. On this server, there is a script that can be executed from the front end server, over ssh. My script will then check to see if the environment variables it needs are loaded correctly because I rely heavily on them in the script itself.

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

This works, although not the way I want things to work. As the connection is established, the ./profile isn't loaded just using exec('source /home/user/.profile'); doesn't work, of course. Since the script is already running. That's why the script starts like this:

#!/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脚本:

That helper script is a ksh-script:

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

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

loading the profile, and calling the first script again. I want this second script gone, I find it silly... needing a second script to run the first. I know that it is possible to set environment values with proc_open, but rewriting the .profile as an array souds even sillier. I also tried to proc_open a shell, load the profile and run the script again from within itself. Only to find that the script keeps calling itself, leading me to believe the profile isn't loaded at all.

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

Here's my attempt so far:

#!/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);
?>

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

I had no luck with this so far, can anyone tell me if I'm forgetting something here? What am I doing wrong? Am I overlooking something here?

推荐答案

我没有ksh,但是我已经成功地使用bash了.

I don't have a ksh, but I've managed to do it with 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆