Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7684925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:08:21+00:00 2026-05-31T19:08:21+00:00

Here’s the situation: I wrote a back end application, that runs on a certain

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:08:22+00:00Added an answer on May 31, 2026 at 7:08 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the situation, i want to have a user that can enter time on
Here's a coding problem for those that like this kind of thing. Let's see
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here's my situation. I've noticed that code gets harder to maintain when you keep
Here is a script I can't get to work correctly. It's purpose is to
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is the scenario: I'm writing an app that will watch for any changes
Here's an interesting problem. On a recently installed Server 2008 64bit I opened IE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.