【问题标题】:Apache Ant - start/stop win services - Access is deniedApache Ant - 启动/停止 win 服务 - 访问被拒绝
【发布时间】:2013-10-03 21:25:15
【问题描述】:

我有 Apache Ant 文件 (build.xml),它被设置为创建我的生产服务器的自动备份。它设置为停止 Apache 服务器和 MSSQL2000 - 备份所有内容并再次启动这两个服务(及其依赖项)。它在旧的 WIN 2008 服务器下可以正常工作,但在 WIN8 下不行。当执行 .bat 文件以运行 Ant 进程时,它会打开 CMD(应该如此),但没有正确执行停止/启动操作。我收到“访问被拒绝”。回应。

这是执行 .bat (Ant) 文件时的 CMD 输出:

assemble:

stop.server.apache:
     [exec] System error 5 has occurred.
     [exec]
     [exec] Access is denied.
     [exec]
     [exec] Result: 2

stop.server.ms-sql:
     [exec] No valid response was provided.
     [exec] The following services are dependent on the MSSQLSERVER service.
     [exec] Stopping the MSSQLSERVER service will also stop these services.
     [exec]
     [exec]    SQLSERVERAGENT
     [exec]
     [exec] Do you want to continue this operation? (Y/N) [N]:
     [exec] Result: -1
     [exec] System error 5 has occurred.
     [exec]
     [exec] Access is denied.
     [exec]
     [exec] Result: 2

实际负责启动/停止服务的 build.xml 部分如下:

....... 代码开头

<macrodef name="service">
    <attribute name="service"/>
    <attribute name="action"/>
    <sequential>
        <exec executable="cmd.exe">
            <arg line="/c net @{action} '@{service}'"/>
        </exec>
     </sequential>
</macrodef>

<target name="start.server.ms-sql">
    <service action="start" service="MSSQLSERVER"/>
    <service action="start" service="SQLSERVERAGENT"/>
</target>

<target name="stop.server.ms-sql">
    <service action="stop" service="SQLSERVERAGENT"/>
    <service action="stop" service="MSSQLSERVER"/>      
</target>

<target name="start.server.apache">
    <service action="start" service="Apache2.2"/>
</target>

<target name="stop.server.apache">
    <service action="stop" service="Apache2.2"/>
</target>

.......更多代码在这里

我已经启用了所有磁盘/文件夹权限以确保它不会受到影响(我知道 Vista 及更高版本的权限发生了变化。我还研究了Apache Ant documentation,但我找不到任何关于停止/启动服务的信息具有提升的权限。

有什么想法吗?

注意:Apache2.2 和 MSSQL 从驱动器 W:\ 运行,而 Win OS 从 C: 运行。我的 Ant 路径设置正确,但我认为这可能会导致权限访问问题。

【问题讨论】:

    标签: ant uac


    【解决方案1】:

    此错误与 ant 无关(直接)。 Windows8(实际上也是 Windows Vista 和 7)有 UAC。即使用户使用管理帐户登录,也无法启动服务。您将拥有我们run-as 管理员。通常这可以作为右键菜单使用。

    要获得相同的脚本,您可以禁用 UAC。 (这里是instructions for windows 7 )。 Windows 8 可能类似指令。

    这个问题的一些详细讨论,可能对你有用:How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?

    【讨论】:

    • 感谢您的回答。第一个建议 - UAC - 我已经设置为最小值(不要通知),第二个 - 指向我昨天已经尝试过的另一篇文章的链接,我无法让它工作。但我认为你在正确的轨道上事实上,Win8 似乎不允许启动这些服务。一定有另一个窗户卡在某个地方。还能是什么?
    【解决方案2】:

    所以... 24 小时后我找到了解决方案。 显然,这种行为(拒绝访问停止/启动任何 win 服务并导致 系统错误 5 发生示例here)是所有较新的 WIN 操作系统的本机。正如我发现的那样,在执行 .bat 文件时似乎没有直接从 CMD 提升用户权限的直接方法,因此随后的每个命令(即:停止或启动服务)都会因以下原因而失败缺乏用户权限。

    我的解决方案

    1) 下载Elevation PowerToys 2.0(这只是一个可执行文件,它创建一个包含多个文件的文件夹“Elevation”)

    2) 从新创建的“Elevation”文件夹中双击 InstallAllPowerToys.cmd 文件(这实际上为您安装了附加选项,以便您在 CMD 中使用“elevate”一词来运行任何 . bat文件直接以管理员身份(不会被win窃听)

    3) 转到您的控制面板 -> 用户帐户 -> 更改用户帐户控制设置 -> ..将滑块一直拉到“从不通知”级别。 (这确保了“以管理员身份运行”弹出框的抑制)

    4) 创建一个包装器 .bat 文件,该文件将以提升的权限执行,即这是一个我称为 backup.bat 的 bat 文件,其内容如下(请注意,在这种情况下,我的网站文件夹位于 W:\ 驱动器上):

    elevate cmd /k "w:\mywebsite\antbackup.bat"
    

    5) 使用对 bild.xml 文件的实际 Apache Ant 调用创建另一个 .bat 文件(包含所有详细信息 - 应该停止或启动哪些服务以及应该在哪里停止将我的文件压缩和备份,等等...)。该文件可以称为 antbackup.bat

    ant -buildfile w:\mywebsite\build.xml antbackup
    pause
    

    6) 双击backup.bat 文件(让你的计划任务来做,交叉手指,希望有好的结果:)

    这对我有用。它也可以帮助其他人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      相关资源
      最近更新 更多