PHP exec $PATH variable missing elements

20,070

Solution 1

Environment variables on Mac OS X are set by differing mechanisms depending on how your code, or its parent process, was launched. To insure that items launched from an interactive shell and items launched by the WindowServer have the same path, you need to keep ~/.MacOSX/environment.plist in sync with what is set in .profile (or .cshrc).

Solution 2

Try executing this before you call exec:

putenv("PATH=" .$_ENV["PATH"]. ':/usr/local/git/bin:/usr/X11/bin');

Solution 3

What does:

php -r 'print getenv("PATH");'

give you?

It's likely the shell that PHP spawns (probably sh instead of bash) isn't getting the same environment that you have at the command line. You don't say how you're running your exec command.

This will show you which shell is being run:

php -r 'echo shell_exec("echo $0");'

You may need to use the putenv command or determine whether your path needs to be set in /etc/profile, ~/.profile or ~/.bashrc in order for it to be picked up.

Share:
20,070
Eric Cope
Author by

Eric Cope

Digital IC Design Engineer by day. Web Developer by night. My skillset includes: Digital Signal Processing: Hardware based and firmware based. HDLs and related: Verilog, SystemVerilog, PSL Scripting: Bash, CSH, Perl, Javascript, Makefiles, Matlab Software/Firmware Languages: C/C++, Python, PHP Frameworks: CodeIgniter, jQuery General Interests: Continuous Integration for Software and Hardware

Updated on September 21, 2020

Comments

  • Eric Cope
    Eric Cope over 3 years

    When I echo $PATH on my command line, it returns

    /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Applications/MAMP/Library/bin:/usr/local/git/bin:/usr/X11/bin
    

    When I execute this php code

    exec('echo $PATH; whoami; less /etc/paths; 2>&1')
    

    I get

    string 'echo $PATH; whoami; less /etc/paths; 2>&1' (length=56)
    array
      0 => string '/usr/bin:/bin:/usr/sbin:/sbin' (length=29)
      1 => string 'eric' (length=4)
      2 => string '/usr/bin' (length=8)
      3 => string '/bin' (length=4)
      4 => string '/usr/sbin' (length=9)
      5 => string '/sbin' (length=5)
      6 => string '/usr/local/bin' (length=14)
      7 => string '/Applications/MAMP/Library/bin' (length=30)
      8 => string '/usr/bin:/bin:/usr/sbin:/sbin' (length=29)
    

    This is on Mac OS X. Can anyone tell me why my last two path elements are missing?

  • Eric Cope
    Eric Cope almost 14 years
    if I use getenv("PATH"), it returns the proper path with my appending it.
  • SourceSeeker
    SourceSeeker almost 14 years
    @Eric: Try setting and exporting ENV in the environment that you're running your script from (before you run it). For example: export ENV=/etc/profile or export ENV=/home/username/.profile. (If you use /.bashrc, you'll need to put code in it that prevents sh from running Bash-specific code that it can't understand.) The Bourne shell only sources those files automatically when it's a login shell.
  • geerlingguy
    geerlingguy over 5 years
    For example, I just needed to do putenv("PATH=" . getenv('PATH'));