Run window system command from Gradle task failed - Access is Denied


I am having issue with creating task to run a window system command to stop a window service.

task startMyService(type: Exec) {
executable = ‘sc’
args = [‘stop’, ‘myServiceName’]
doLast {
println ‘startMyservice done’

Here are my steps

  1. open a Window Command Prompt as Administrator
  2. execute the following command to make sure I have the permission. myService is started
    sc start myService
  3. after manually stop myService, execute the following command and I got error
    gradlew startMyService

Task :startMyService FAILED
[SC] StartService: OpenService FAILED 5:

Access is denied.

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:startMyService’.

Process ‘command ‘sc’’ finished with non-zero exit value 5

It seems to me is the Gradle task doesn’t have the permission to run a window system command. Does anyone run into such issue and any solution/suggestions?


An observation with respect to Windows10 EUAC setting: The gradle process inherits privileges from daemon. If daemon started from a process with out elevated access, running gradle from window with elevated access does not help.