Load .profile with proc_open ()

Here's the situation: I wrote a back-end application that runs on a specific server. There is a script on this server that can be executed from an external server, over ssh. My script then checks 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 it to work. When the connection is established, the. / profile isn't loaded, but using exec('source /home/user/.profile');

doesn't work of course. Since the script is already running. This is why the script starts like this:

#!/to/php/bin/php -n
        exec('/absolute/path/to/helperscript '.implode(' ',$argv),$r,$s);
        if ($s !== 0)
            die('helper script fails: '.$s);


This script helper is ksh-script:

source /.profile


Loading the profile and calling the first script again. I want this second script to go away, I find it silly ... it takes a second script to run the first. I know it is possible to set environment values ​​with proc_open, but rewriting .profile as a souds array is even more foolish. I also tried proc_open

shell, load the profile and run the script again from within itself. Only to find that the script keeps calling itself makes me believe the profile isn't loaded at all.

Here's my attempt:

#!/to/php/bin/php -n
    if (!$_SERVER['VAR_FROM_PROFILE'] && $argv[1] !== 'fromself')
        $res = proc_open('ksh',array(array('pipe','r'),array('pipe','w'),array('pipe','w')),$pipes);
        fwrite($pipes[0],'source /home/user/.profile & '.$argv[0].' fromself');
        fclose($pipes[0]);//tried using fflush and a second fwrite. It failed, too
        echo stream_get_contents($pipes[1]);


I am still out of luck, can anyone tell me that I forgot something? What am I doing wrong? Did I forget something?


source to share

1 answer

I have not ksh

, but I managed to do it with bash



export VAR_FROM_PROFILE="foobar"



#!/usr/bin/php -n
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");
  echo "Output:\n";
  echo stream_get_contents($pipes[1]);
  echo "\n";
print "Got env var {$_SERVER['VAR_FROM_PROFILE']}\n";
// Useful part of the script begins



[galymzhan@dinohost ~]$ ./test.php 
Got env var foobar




All Articles