46

I want to set the Recovery Options on a Windows Service I'm installing on a Windows Server 2003. I know this is possible to do manually, but I want to set the Recovery configuration when I install the service.

I use SC script to do this:

SC create MyService displayname= "MyService" binpath= "C:\Program Files\MyService\MyService.exe" start= auto

SC failure MyService reset= 86400 actions= restart/1000/restart/1000/run/1000

SC failure MyService command= "C:\Program Files\Myservice\MyService.exe"

The problem is when the first whitespace is hit, it cuts off the path and takes the rest of the path as input parameters:

screendump here

So... in the properties dialog for the service (run -> services.msc -> right-click MyService -> Properties -> Recovery tab) I select "Subsequent failures -> Run a Program

As you can see I have put "" around the path in the command. How do I get to make whitespaces in the path? I know I can just make a path with no whitespaces, but that's not the point :-)

user2110298
  • 567
  • 2
  • 5
  • 9

1 Answers1

20

This syntax seems to work:

sc failure MyService command= "\"c:\program files\myservice\myservice.exe\""
Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
  • Ah, that's it! Thank you very much. Of course it was the backslash. And thank you for seeing past the path screwup. – user2110298 Feb 26 '13 at 10:38
  • As an interesting side note, the protection character in Windows command line is `^`. But what to expect when it is *needed* to have a space after `=` (sigh) – Matthieu Apr 07 '15 at 14:16
  • @Matthieu: the caret protects characters from the command-line shell's command line processing, so it is useful to protect, e.g., the pipe character. In this case we're dealing with the application's command line processing, which by default (i.e., if the application uses Microsoft's C runtime library to parse the command line, which does seem to be the case here) uses backslashes. – Harry Johnston Apr 07 '15 at 22:52