【问题标题】:Shell script doesn't properly execute from ruby CGI scriptShell 脚本无法从 ruby​​ CGI 脚本正确执行
【发布时间】:2012-03-29 21:37:14
【问题描述】:

我有一个调用 shell 脚本的 ruby​​ cgi 脚本。

shell 脚本执行 git pull。

当我从命令提示符运行 shell 脚本时,它可以工作。

但是当我从 ruby​​ cgi 脚本运行它时,它会执行脚本,但不会发生 git pull。

我猜这可能与权限有关,但我不知道如何解决它。

ruby 脚本是:

#!/usr/local/rvm/rubies/ruby-1.9.3-p125/bin/ruby
require "cgi"
git_pull = `sh /github/do_git_pull.sh`
move_apanels = `sh /github/move_apanels.sh`

puts "Content-type: text/html\n\n"
puts "<html><body>We've done the following:<ul>"
puts "<li>#{git_pull.to_s}</li>"
puts "<li>#{move_apanels.to_s}</li>"
puts "</ul></body></html>"

而shell脚本是:

#!/bin/bash
sudo sh -c cd /github
sudo sh -c git pull origin master
echo "Git Pull Completed"

两个文件都有 chmod 777

有什么想法吗?

【问题讨论】:

    标签: ruby bash shell cgi


    【解决方案1】:

    这样做:

    sudo sh -c cd /github
    

    仅在 sh 命令期间更改 PWD。它确实影响当前的shell。你需要 cd 和 git pull 在同一个子shell中:

    sudo sh -c 'cd /github && git pull origin master'
    

    【讨论】:

      【解决方案2】:

      在您的脚本上设置 777 不会削减它。尝试找出您的 ruby​​ 脚本在其下执行 shell 脚本的用户。由于 git 使用 SSH 密钥进行身份验证,并且通常您的 SSH 密钥只能由您使用,因此如果其他用户尝试执行 git pull,则 git pull 将失败。

      查看this关于如何以不同用户身份运行 shell 脚本的问题。

      还要确保目标环境中的PATH 设置正确且可访问(如果您运行 chroot 的 Web 服务器)。

      【讨论】:

        猜你喜欢
        • 2018-08-28
        • 2022-01-27
        • 1970-01-01
        • 2016-09-29
        • 1970-01-01
        • 1970-01-01
        • 2013-06-13
        • 2017-06-24
        • 2023-04-04
        相关资源
        最近更新 更多