简体   繁体   English

PHP:通过HTTP和命令行运行脚本时的结果不同

[英]PHP: different result when running script via HTTP and in command line

I have a GIT repo and i'm using PHP script to pull from master branch and update files on server. 我有一个GIT仓库,我正在使用PHP脚本从主分支中提取并更新服务器上的文件。 It's really simple solution just to test it out. 这是一个非常简单的解决方案,只是为了测试它。 I wanted to add a webhook on github to send callback after successful push so i could pull from this repo. 我想在github上添加一个webhook,在成功推送后发送回调,这样我就可以从这个回购中获取。

Script runs both ways 脚本以两种方式运行

  • from command line 从命令行
  • from HTTP request 来自HTTP请求

The difference is that when i run it from HTTP it doesn't pull new changes, just displays that it is up to date and doesn't do anything even if i'm 100% sure that there are new changes in repo. 不同之处在于,当我从HTTP运行它时,它不会提取新的更改,只显示它是最新的,并且即使我100%确定repo中有新的更改也不会执行任何操作。 Output comes from GIT command so i'm sure it works and apache has proper rights to run that script file and commands which are used inside it. 输出来自GIT命令,所以我确信它有效,并且apache具有运行该脚本文件和在其中使用的命令的适当权限。

When i run it via command line (through the same script or just clean git command) everything's fine, script pulls new changes and display standard GIT output. 当我通过命令行运行它(通过相同的脚本或只是干净的git命令)一切都很好,脚本拉出新的更改并显示标准的GIT输出。

PHP code: PHP代码:

echo "Pulling changes from master branch...\n";
$result = shell_exec('cd /var/git-test && git reset --hard HEAD && git pull');
if($result !== null){
  echo $result;
}else{
  echo $result;
  echo "Error in Git pull comand";
}

How can i fix that? 我该如何解决这个问题?

sudo -u www-data php deploy.php showed that www-data user didn't have ssh key registered on github and pull couldn't be performed. sudo -u www-data php deploy.php显示www-data用户没有在github上注册ssh密钥并且无法执行pull。 I couldn't switch to www-data user so i generated his key without switching to it like i did with running the script sudo -u www-data ssh-keygen -t rsa . 我无法切换到www-data用户,因此我生成了他的密钥,而不是像运行脚本sudo -u www-data ssh-keygen -t rsa那样切换到它。 After registering his key on github it works like a charm. 在github上注册他的密钥后,它就像一个魅力。

Also pro tip! 也是专业提示! shell_exec() doesnt show all output which was visible when i run the script as another user in command line. shell_exec()不显示当我在命令行中以另一个用户身份运行脚本时可见的所有输出。 It was really confusing and cost me a lot of time to figure out where the error was. 它真的很混乱,花了我很多时间来弄清楚错误的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM