简体   繁体   中英

Execute bat file as a windows service

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.

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

OR

if you want run it at startup.

As an example, on Windows 8

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

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

On Windows 2012 Server

sc create myservice binPath=C:\some\path\to\myservice.bat

and to remove:

sc delete myservice

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM