2

OS: Windows 10

I have a batch file that syncs two local folders.

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

0
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 ----------------------------------------------------------
    <Blank/Nothing>


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


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 ---------------------------------------------------------
    <Blank/Nothing>


09) Enviroment ------------------------------------------------------------
    <Blank/Nothing>


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"

:loop
"%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.

2
  • 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 '20 at 20:59
  • @tomatoeshift You may need to change the path where you set the file log. Or run NSSM as Admin using runas
    – iTwasnTme
    Apr 16 '20 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.