如何在 OSX 上为您安装的添加的 PHP 版本设置用户 .bash_profile 之外的系统 $PATH
好吧,这是我的问题。我正在尝试编写一个 php 脚本来运行以下类型的命令。
exec("$(which php) -f /path/to/script.php >> /path/to/log.log 2>&1 &");
问题是,我正在运行 OSX 10.5 leopard 并使用 Entropy PHP 包。这将在 /usr/local/php5/bin 下创建 php。与 OSX 一起安装的普通 php 位于 /usr/bin 下。因此,当 PHP 执行此命令时,它使用了错误的 PHP 可执行文件。现在我知道如何通过将 .bash_profile 添加到 $PATH 的开头来更改 .bash_profile 中的 $PATH,以便系统采用我想要的 PHP,而不是默认的 PHP。问题是,当从脚本运行 php 时,它不会在我的用户下执行。所以它没有更新的 $PATH 设置。
我还考虑过在 /etc/paths 和 etc/paths.d/ 中设置它,但是它们都将路径附加到末尾,我需要将它附加到开头,以便我的脚本将使用正确的 PHP 可执行文件。
我知道我可能可以运行另一个 exec 命令并设置路径,但这只是本地环境问题,当代码投入生产时我不需要这个设置。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种选择是为所有用户设置全局路径。请参阅此答案了解一些示例。
例如,如果运行命令的用户使用 bash 作为其 shell,您可以编辑 /etc/bashrc 添加一行,例如:
另一种选择是修改 /etc/launchd.conf (请注意,它确实默认情况下 Leopard 上不存在,需要
csh
setenv
语法)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:Another option is to modify /etc/launchd.conf (note that it does not exist on Leopard by default and expects
csh
setenv
syntax)您是否尝试过在 exec 函数之前执行此操作:
Have you tried executing this before your exec function:
如果我是你,我会专门为 exec 和 shell 调用制作一个包装脚本:
这样你就可以使用指向 php 的环境变量,它将在大多数 nix 系统上工作。
现在,这仍然无法让您在 OS X 安装上正确设置,因为您将硬编码路径或遵循链接到的 nimrodm 答案。
IF i were you i would make a wrapper script specifically for exec and shell invocation:
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.