2

I have a windows batch file which i want to execute as a service. I have found app like alwaysRun but i want to use windows in-build app for this purpose. Can anyone please suggest.

My Use Case is : - I have a batch file which will be executing after every 10 secs. So i have created a normal batch file which calls this bat file and sleeps for 10 secs. So i want to make this second bat file as a service. So that it is called once and when the windows reboots.

This file should be called as a service.

@echo off
:begin
CALL dummyfile.bat
timeout /t 1
goto begin

Please suggest.

2
  • This is impossible. Commented Sep 26, 2017 at 8:16
  • 1
    @KonstantinL No it is not impossible at all, but it is silly idea though.
    – Gerhard
    Commented Sep 26, 2017 at 9:03

3 Answers 3

6

I would not do that. You could run the first batch file from a scheduled task.

OR

If you want it to run at startup,

As an example, on Windows 8 you could

Create a VBS file that will completely hide your batch file.

hideme.vbs

Set MyScript    = CreateObject("WScript.Shell")
MyScript.Run "C:\wherever\script\is\batch.cmd", 0, False

It can be launched as cscript hideme.vbs

Then open Start / Run and type shell:startup and press enter. Paste a shortcut of the VBS file here.

This will let VBS file call the second batch file in hidden mode each time the PC starts up.

EDIT In order to kill it, you need to create another batchfile which you can run to find the cmd.exe that is running in the background.

In your original batch file, create a title at the very beginning after @echo off

@echo off
title LOOP
:begin
CALL dummyfile.bat
timeout /t 1
goto begin

Now in your new batchfile, let's call it killLOOP.cmd you add this:

taskkill /F /FI "WindowTitle eq  LOOP" /T

This will search for a process with Window title LOOP, then kill it. Just run it when you want to close it. now in your

10
  • In the Scheduled task settings the script cannot be run before every 1 min.So i cannot try that as i need to run it after every 10 secs.
    – Sunny
    Commented Sep 26, 2017 at 9:28
  • @IftekharKhan then use the method I mentioned in the answer.
    – Gerhard
    Commented Sep 26, 2017 at 9:41
  • Set MyScript = CreateObject("WScript.Shell") MyScript.Run "C:\Users\Documents\Python Scripts\Data_Extractor_Service.bat", 0, False
    – Sunny
    Commented Sep 26, 2017 at 10:05
  • This is giving me an error that the system cannot find the file specified.
    – Sunny
    Commented Sep 26, 2017 at 10:05
  • put the vbs file in the same directory as your batch file, and just do Set MyScript = CreateObject("WScript.Shell") MyScript.Run "Data_Extractor_Service.bat", 0, False
    – Gerhard
    Commented Sep 26, 2017 at 10:07
-1

have you ever looked at the native Windows program called sc.exe ? It allows you to run a program as a service.

Here's some Microsoft documentation on it: https://support.microsoft.com/en-us/help/251192/how-to-create-a-windows-service-by-using-sc-exe

sc [Servername] Command Servicename [Optionname= Optionvalue...]

In particular, you could use the Create Command to create a new service:

Create Creates a service (adds it to the registry).

So the simplest syntax would be something like:

sc Create myservice binPath=C:\some\path\to\myservice.bat
0
-1

I executed batch file as a service, but it didn't work.

NSSM package resolved my problem. here are the steps to follow:

1- Download NSSM package

2- Add NSSM Directory path in Environment Variables Path

3- Execute command like:

nssm install Service_Name

3.1 After executing command mentioned in step 3, it will show a popup to provide program file path. provide batch file path like "C:/Test/test.bat"

3.2 Click on install and now batch file will be executed as a window Service.

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