سؤال

I have Apache Ant file (build.xml) which is set to create automatic backup of my production server. It is set to stop Apache server & MSSQL2000 - backup everything and start both services (and their dependencies) again. It is working fine under the old WIN 2008 server but not under the WIN8. When the .bat file is executed to run the Ant processes it opens the CMD (as it should) but none of the stop/start operation execute properly. I am getting "Access is denied." response.

This is the CMD output when .bat (Ant) file is executed:

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

the actual build.xml portion responsible for starting/stopping services is as follows:

....... beginning of the code

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

....... more code here

I already enabled all disk/folder permissions to make sure it is not being affected (I know there has been permission changes from Vista and up. I also studied the Apache Ant documentation but I could not find anything about stopping/starting services with elevated privileges.

Any idea ?

NOTE: the Apache2.2 and MSSQL are running from the drive W:\ and the Win OS is running from C:. My Ant paths are set correctly but I thought this may be contributing to the privilege access issue.

هل كانت مفيدة؟

المحلول 2

So... 24 hours later I found the solution. Apparently this behavior (Access denied to stop/start any win services and the consequent System error 5 has occurred sample here) is native to all newer WIN OS. As I found out, there seems to be no straight forward way to elevate the user privileges directly from CMD when executing .bat file so every command which follows (i.e.: stop or start service) will fail due to a lack of user privileges.

My solution

1) Download the Elevation PowerToys 2.0 (this is just an executable which creates a folder "Elevation" with several files)

2) from the freshly created "Elevation" folder double-click InstallAllPowerToys.cmd file (this actually installs the additional options for you to use the word "elevate" in the CMD to run any .bat file directly as an administrator (without being bugged by win)

3) go to your Control Panel -> User Accounts -> Change User Account Control Settings -> ..bring the slider all the way down to "never notify" level. (this ensures the suppression of the "run as administrator" popup boxes)

4) create a wrapper .bat file which will be executed with elevated privileges i.e this is a bat file which I called backup.bat and its content is as follows (note my website folder in this case is located on W:\ drive):

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

5) create another .bat file with the actual Apache Ant call to bild.xml file (with all the details - which services should stop or start and where should be my files Zipped and backed-up, etc...). This file could be called antbackup.bat:

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

6) double-click the backup.bat file (of have your scheduled tasks do it, cross your fingers and hope for good results :)

This worked for me. It could help someone else as well.

نصائح أخرى

This error has nothing to do with ant (directly). Windows8(actually windows Vista and 7 too) has UAC. Services cannot be started even if the user is logged in with administrative account. You will have us run-as administrator. Typically this is available as right-click menu.

To get the same to a script you can disable UAC. (Here is the instructions for windows 7 ). Windows 8 may similar instruction.

Some detailed discussions in this question, which may be useful to you : How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top