dosin84644
2011-04-21 18:18
浏览 47

如何在OSX上为用户.bash_profile设置系统$ PATH以获取您安装的已添加的PHP版本

Ok so here is my problem. I am trying to write a php script that will run the following type of command.

exec("$(which php) -f /path/to/script.php >> /path/to/log.log 2>&1 &");

The problem is, I am running OSX 10.5 leopard and using the Entropy PHP package. This creates php under /usr/local/php5/bin. The normal php that is installed with OSX in under /usr/bin. So when PHP executes this command it is using the wrong PHP executable. Now I know how to change the $PATH in my .bash_profile by adding it to the begining of the $PATH so that the system takes the PHP I want other then the default PHP. The problem is, when running the php from a script it doesn't do it under my user. So then it doesn't have the updated $PATH settings.

I have also looked into setting it in /etc/paths and etc/paths.d/ but these both append the path to the end, I need it to append to the beginning so that my script will use the right PHP executable.

I know I probably could run another exec command and set the path but this is only a local environment issue and when the code goes to production I won't need this set.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanpen9294 2011-04-21 18:25
    已采纳

    One option is to set the global path for all users. See this answer for a few examples.

    For example, if the user running the command is using bash as its shell you may edit /etc/bashrc adding a line like:

    export PATH="$PATH:/more/paths:/
    

    Another option is to modify /etc/launchd.conf (note that it does not exist on Leopard by default and expects csh setenv syntax)

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanniling0018 2011-04-21 18:24

    Have you tried executing this before your exec function:

    $my_new_path = '/usr/local/php5/bin';
    putenv( "PATH=" . $my_new_path . PATH_SEPARATOR . getenv('PATH') );
    
    //then run your exec() call
    
    评论
    解决 无用
    打赏 举报
  • dtotuki47568 2011-04-21 18:28

    IF i were you i would make a wrapper script specifically for exec and shell invocation:

    #!/usr/bin/env php
    <?php 
    
    include('/path/to/script.php');
    

    This way you can use the environment variable that points to php, which will work on the majority of nix systems.

    Now that still doesnt get you set right on your OS X install for that you are going to have hardcode the path or follow the answer nimrodm linked to.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题