OS: Windows 10

I have a batch file that syncs two local folders.

robocopy C:\Folder_A C:\Folder_B /E /MIR
timeout 20
Goto loop

When I open it manually it works just fine.

I have installed the batch file as a NSSM service. On initial start of the service, the folders are synced, but after that the script is dead. Upon restarting the service, the initial sync is once again performed once.

Can't I have a loop in a service, or what is the problem


1 Answer 1


You can have a loop by following these screens/steps/settings in NSSM:

01) Application -----------------------------------------------------------
    Path:              ["%windir%\system32\cmd.exe"                ]|...|
    Startup directory: ["C:\path\to-bat-folder"                    ]|...|
    Arguments:         [/C "C:\path\to-bat-folder\Batch_File.bat"  ]|...|

02) Details ---------------------------------------------------------------
    Display name: Test batch loop                                         
    Startup type: Automatic (Delayed Star)

03) Log on ----------------------------------------------------------------
    (•) Local System account [√] Allow services to interact with desktop

04) Dependencies ----------------------------------------------------------

05) Process ----------------------------------------------------------------
    Priority: [Normal  |▼|]  Affinity: [ ] All process
    [√] Console windows          CPUs: |◙|

06) Shutdown --------------------------------------------------------------
    [√] Generate Control-C             Timeout [1500 ]ms
    [√] Send WM_CLOSE to windows       Timeout [1500 ]ms
    [√] Post WM_QUIT to threads        Timeout [1500 ]ms
    [√] Terminate process       

07) Exit actions ----------------------------------------------------------
    Delay restart if applications for runs less than [1500 ]ms
    Action to take when application exits    [Restart application    |▼|]
    other than in response to a controlled
    services shutdown:                        Delay restart by [0  ]ms

08) File rotation ---------------------------------------------------------

09) Enviroment ------------------------------------------------------------

10) I/O -------------------------------------------------------------------
    Input (stdin):   [                                               ]|...|
    Output (stdout): ["C:\path\to-bat-folder\Name_Batch_Act_logs.log"]|...|
    Error (stderr):  ["C:\path\to-bat-folder\Name_Batch_Err_logs.log"]|...|

Use this code in your bat:

@echo off && cd /d "%~dp0"

"%windir%\system32\robocopy.exe" C:\Folder_A C:\Folder_B /E /MIR
"%windir%\system32\timeout.exe" /t 20 & goto :loop 

About what's wrong, well, this needs to see what settings were used

It is not possible to say what it is without even seeing them.

  • well now it stopped working as it should. While the script is running and new files are created, NSSM has the output ERROR 5 (0x00000005) Copying File Access is denied. The local user has admin rights, so that is not the problem. When the service is restarted, the previously unaccessible files are copied. Apr 16, 2020 at 20:59
  • 1
    @tomatoeshift You may need to change the path where you set the file log. Or run NSSM as Admin using runas
    – Io-oI
    Apr 16, 2020 at 21:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .