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

“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!!!

  • 写回答

2条回答 默认 最新

  • 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退