Creating a service to run RoboCopy

13,896

I have been able to setup such a thing a few year ago...but cannot remember how ! So i have powered back on my old VM to check.

I have used the srvany.exe utility that comes with the Windows 2003 ressource kit.

This utility is not really supported on recent version of Windows but works on Windows 2008 R2.

From the previous link, note this important point :

Note however that SC is NOT a replacement for SRVANY! SC will help you create/install a service, but it will not allow you to run a regular, non-service executable as a Windows Service like srvany.exe will.

This is why you get your error message. The Robocopy command is executed when the service starts, but then it crashes because it is not designed to run as a Windows Service.


  1. Download and install rktools.exe on your workstation, and then copy only the needed file srvany.exe somewhere on your server (let's say c:\Tools).

  2. Then create the Windows Service for srvany :

    sc create Robocopy-Service binPath= "C:\Tools\srvany.exe" start= auto
    
  3. Open the registry and go to: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Robocopy-Service

  4. Create a new key called Parameters

  5. Under that new key, create 3 new String Values :

    • AppDirectory : c:\windows\system32
    • Application : c:\windows\system32\robocopy.exe
    • AppParameters : c:\source c:\dest /MIR /MON:1

Finally start the service named Robocopy-Service and everything should work fine.

Now, from here, you can still automate things in a Batch file, but you will have to use reg.exe (or regedit.exe) to manipulate Registry settings.

I've also found, in my bookmarks, the original link that helped me : https://plus.google.com/112485889729268615636/posts/bH8rSDo5ocC

Share:
13,896
Klee
Author by

Klee

I am a software developer based in Newcastle. My interest is primarily in the art of development over individual technologies. I am somewhat evangelical about clean code. I'm also a regular attendee, co-organiser and occasional presenter at the Newcastle Coders group.

Updated on September 18, 2022

Comments

  • Klee
    Klee over 1 year

    I'm attempting to put together a batch file that will set up a robocopy task as a service in response to user input. The basic idea being that the user will input MyRobocopyBatchFile.bat sourceFolder destinationMachine and from then on the contents of the folder mirrored with a known folder on the destinationMachine. Service will be set to start automatically so it will run at startup. For this I've taken queues from this question

    The target environment for this is WindowsServer 2008 R2

    My intention for how to do this is

    set destination=\\%2\RunSheets
    set source=%~dp0%1
    echo Setting source to %source%
    echo Setting destination to %destination%
    set serviceName=RunSheetCopy%2
    
    sc create %serviceName% binPath= "c:\Windows\System32\robocopy.exe %source% %destination% /MIR /MON:1 /v /log:C:\Logs\RoboCopy\%serviceName%.log /LEV:1" start= auto DisplayName= %serviceName% 
    
    sc start %serviceName%
    

    with user input something like:

    MyRobocopyBatchFile.bat .\RunSheets 10.20.30.40
    

    The problem that I am facing is that when the batch file gets to starting the service it gives me an error message. The same error message occurs when starting with NetStart or via the services window. The error message is:

     [SC] StartService FAILED 1053:
    
     The service did not respond to the start or control request in a
     timely fashion.
    

    Despite the error message robocopy is syncing the directories but it does not continue in monitor mode.

    If any help with how to get robocopy running as a service would be greatly appreciated.

    N.B. The product manager is very keen on the idea of a service.

    UPDATE: Since there was no way to do this in a mechanism that would make the PM happy (SrvAny, being legacy was not an option), I ended up going with hacking together a service wrapper for RoboCopy, Its not the solution that I would have liked but it will do the job.

    • Ryan Ries
      Ryan Ries about 10 years
      This is kludgy. I'd look into creating a DFS replicated file share and having your users save their files there.
    • Klee
      Klee about 10 years
      It is kludgy, unfortunately the requirements of the project, and the problem that the solution is trying to solve, dictate the solution here to a certain extent.
  • Spence
    Spence about 10 years
    I'm a bit loathe to give advise because I don't really think what the OP is looking for is a good solution, but with that caveat in mind, I'd argue that NSSM (nssm.cc) is a lot better service manager than the old SRVANY for the OP's needs. NSSM has a much richer feature set and is being actively maintained.
  • krisFR
    krisFR about 10 years
    I was also confused when i read the question and wrote my answer. But well, finally i've decided to only answer the question, but keeping in mind to avoid as mush as possible the use of third party software (at least non Microsoft third party software). AlwaysUp (non-free) could also be an alternative. @Ryan Ries is also right about DFSR.