3

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.

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]);
    }

That helper script is a ksh-script:

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

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?

hakre
  • 193,403
  • 52
  • 435
  • 836
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • Do you really need to load environment variables from `.profile`? I mean, why not just hardcode these variables into PHP script? How are you using these variables inside PHP script? – galymzhan Mar 23 '12 at 18:29
  • 1
    Yes, I do. the `.profile` contains 100+ variables and aliases. To give you an idea: my script calls two other scripts, that both rely on the environment variables, too. The environment variables they need depends on the data they are processing. I could pre-process this data and load the appropriate variables accordingly but that would take me ages to debug. The script should be able to run on any of our company's servers, too. Hard coding would mean writing a script for every environment. That just seems even clumsier than using the korn shell script approach I'm using now. – Elias Van Ootegem Mar 24 '12 at 01:53

1 Answers1

4

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

Output I've got:

[galymzhan@dinohost ~]$ ./test.php 
Output:
Got env var foobar
galymzhan
  • 5,505
  • 2
  • 29
  • 45