douzhun5971
douzhun5971
2012-10-06 07:52
浏览 93
已采纳

“which”命令在PHP中不起作用

I have two remote servers that need to have a certain codebase in sync. I have it all set up on the first, but while setting it up on the second, I ran into a problem:

They don't have hg installed in the same location. I can't just run hg <command> because it returns the error:

sh: hg: command not found

So I had been using the full path, but that won't work with two separate hg locations.

I thought it would be clever to run which hg and use the path from the response, so here's the new code:

$hg = trim(`which hg`, "
");
$output = `{$hg} pull -u`

But $hg is NULL, so that doesn't work! I can ssh into the 2nd server and see that which hg definitely does work. I even appended 2>> path/to/log to see if there were any errors with the which command, but there was not. I made sure it was writing to the log, so it wasn't related to that.

I am not running in safe mode, and I am definitely allowed to run shell_exec because other commands work.

I know I could just create a symlink so they both had the same path and just hardcode the path, but it's driving me crazy why shell_exec('which hg') isn't working!!!

图片转代码服务由CSDN问答提供 功能建议

我有两个远程服务器需要同步某个代码库。 我把它全部设置在第一个,但在第二个设置时,我遇到了一个问题:

他们没有安装 hg 在同一个地方。 我不能只运行 hg&lt; command&gt; ,因为它返回错误:

sh:hg:command not found

所以我一直在使用完整路径,但这不适用于两个单独的 hg 位置。

我认为运行 hg 并使用响应中的路径会很聪明,所以这里是新代码:

  $ hg = trim(`hg`,“
”); 
 $ output =`{$ hg} pull -u` 
   
 
  

但是 $ hg NULL ,所以这不起作用! 我可以ssh到第二个服务器,看到哪个hg 肯定能正常工作。 我甚至附上 2&gt;&gt; path / to / log 查看 which 命令是否有任何错误,但没有。 我确定它是写入日志的,所以它与此无关。

我没有在安全模式下运行,我绝对可以运行 shell_exec < / code>因为其他命令都有效。

我知道我可以创建一个符号链接,所以它们都有相同的路径,只是对路径进行硬编码,但它让我疯狂为什么 shell_exec ('哪个hg')无效!!!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongqian8265
    dongqian8265 2012-10-06 07:55
    已采纳

    which hg will only work if hg is already in your path, otherwise it will return the empty string.

    Obviously this is not what you want, since if it were already in your path, you could just use hg.

    If you're using many machines with different configs, you could just create a bin directory in your home directory, add it to your path and make links to the commands you need in that directory. That will allow you to use the same commands on all machines.

    For PHP, you'd need to put it in a directory accessible to the web server (but preferably outside the document root), besides that, you can use the same technique there by giving an absolute path to the common directory where you created your links.

    点赞 评论
  • duankuang1046
    duankuang1046 2012-10-06 07:55

    try

    shell_exec('/usr/bin/which hg');

    instead. $PATH may not be set and that is a typical location of which

    点赞 评论

相关推荐