Execute bat file as a windows service - windows

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. 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

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

Related

Keep cmd.exe console window with specific title open?

in Windows 7 x64 SP1 I need to create a batch file (.bat) which keeps the cmd.exe console window open and has a specific title:
#ECHO OFF
title notepadtest
#ECHO ON
start "" /WAIT notepad
But this batch file keeps opening an infinite number of cmd.exe console windows in an unstoppable loop!
How can I create a batch file (.bat) which creates only ONE cmd.exe console window and keeps it open and has a specific title?
Please ensure that your batchfile is not named notepad.cmd or notepad.bat or anything like any system or external command. Name it something like mynotepad.cmd instead., then try this one please:
#echo off
title notepadtest
start "" /w notepad.exe
cmdline and batch files typically works like this. When a command is issued, it first checks the local path, where the script was launched from for the command, if not found, it will check your environment and system environment. So if you name a batchfile notepad.bat your batch is actually starting itself over and over, instead of finding notepad.exe in the environment variables.
Always name batch files something unique and not system/external command related.
Always use full executable name in a batch, like start "" /w notepad.exe and not start "" /w notepad

How to make SVN log display in the window using batch file and Task Scheduler?

I am trying to make a batch file that will check certain SVN repositories for updates each morning. I want to store local repository names in a file (SVN_check_list.txt) and have the console show me the list. My code, posted below, works when I just run the batch file:
#echo off
echo Checking for updates...& echo.
for /F %%A in (SVN_check_list.txt) do (
echo Checking '%%A'
svn status %%A -u )
pause
However, when I try to run it through Windows Task Scheduler (while I am logged in), it runs the code but does not display anything until the 'pause' at the end. When I turn echo on, it shows the commands (svn status -u) but not the output. How can I make this batch file display the outputs of the svn status command even when I run it with task scheduler?
Try passing cmd as the Program/Script to run in Scheduler with arguments /k "C:\My Batch File Folder\MyScript.bat"
This will launch the console.
I found this solution that seems to work: Run a batch file with Windows task scheduler
Basically:
Action: Start a program
Program/script: cmd
Add arguments: /k "C:\Users\tanderson\Documents\setup.bat"
Start in: C:\Users\tanderson\Documents (No quotes)

Stop CMD opening when bat file launched

I have this code
start /b error.vbs
When I run it, a CMD opens for half a second, then it runs. How would I remove or hide the CMD?
You don't need a batch file to launch a VBScript script. Just run the .vbs file directly with with wscript.exe (if you don't want the console). For example:
wscript error.vbs

My batch file is closing a program too early

Thank you, for those whom took the time to read my question. I am a gamer and would like to execute a few things. First, I would like to Trigger a batch file when I click a program, how do you do that or is it even possible? Basically, activating a game, triggers the batch file.
NOw for the batch file problem, I want to execute Xpadder when I activate games (this is an mmo) and when I close the game I want Xpadder's process/service to close. Ultimately, it's auto trigger,activate,wait,terminate.
That's kind of the process I want it to go if all can be done.
Batch File:
#echo off
start "Blade" "C:\Users\user\Documents\Blade.xpadderprofile" Blade.xpadderprofile
ECHO Blade.xpadderprofile STARTED
start /w "C:\Program Files (x86)\game\games.exe" games.exe
ECHO games STARTED
taskkill /f /im Xpadder.exe
This actually works but the problem is there are two ".exe" files with mmo's. I'll start the game and it would close Xpadder too early because one launcher starts another launcher/client. Xpadder works for the first launcher but the first launcher closes so the game will start. I hope I am explaining myself clear enough.
Reference link: How to automatically close App_A when I close App_B using batchfile
Essentially, this is the same question I have but it's not very clear. What is the batch code to get Xpadder to stay on until the second launcher/client is closed not the first one?
P.S
The game has to open through the launcher then into the second launcher/client or it will not work.
here is the other clients name and path i think:
C:\Program Files (x86)\game\gamer\bin\gam.exe
How about the use of the PsExec, a small MS ulility? Using it, your batch should work:
cmd /c psexec -d "Blade" "C:\Users\user\Documents\Blade.xpadderprofile" Blade.xpadderprofile
start /w "C:\Program Files (x86)\game\games.exe" games.exe
taskkill /f /im Xpadder.exe
The file psexec.exe must be placed in folder enlisted in the system variable WINDIR or PATH, or, otherwise, you should call it with its full path, eg. *C:\Program Files\Others\pstools.exe".
You can add #echo off, salt, pepper or some green Tabasco if you have mon€y for this :D

Building a batch file to run exe files sequentially

I just start to learn how to build batch file. ( on the windows 7 environment)
I want to build the batch file which is able to run .exe files sequentially .
Run batch files sequentially
I was trying to apply above idea but I am not really sure how to apply it
For example, there are three file on the D:/
In "D:/" there are three .exe files.
MyDriver.exe
YouDriver.exe
Mysoftware.exe
And I would like to build batch file which is running three exe files sequentially
Possible scenario is..
Run batch file
Run MyDriver.exe
MyDriver file's install GUI pops up and then user start to install Mydriver
Done with MyDriver.exe
Run YouDriver.exe
YouDirver file's install GUI pops up and then user start to install YouDriver
Done with YouDriver.exe
Run MySoftware.exe
MySofrware install interface pops up and then user start to install MySoftware
Done exit batch file.
I am not really sure if batch files can do it or not...
if it is impossible , is there any other options to build it ???
thanks
You actually don't need to do anything special to make this happen; batch files are synchronous by default, so execution of the batch file will pause when an executable is launched, and resume when it exits. Something as simple as this should do:
#echo off
REM "#echo off" prevents each line from being printed before execution,
REM and is optional
REM "REM" introduces a comment line
D:\MyDriver.exe
D:\YouDriver.exe
D:\MySoftware.exe
Of course, if you're interested in checking the return values of the programs, to see whether they succeeded or failed to install (assuming the installer provides that information), then things become slightly more complicated; if that's what you need, mention it in a comment, and I'll expand my answer accordingly.
This will start each file and wait for it to complete and then launch the next one.
#echo off
start "" /w /b "d:\MyDriver.exe"
start "" /w /b "d:\YouDriver.exe"
start "" /w /b "d:\Mysoftware.exe"
start MyDriver.exe
start YouDriver.exe
start MySoftware.exe
If you want the batch file in a different dir you would have to do:
cd D:\
start MyDriver.exe
start YouDriver.exe
start MySoftware.exe
If you want a more flexible system:
echo Welcome to EXE starter!
set /p dir = DIR:
set /p exe = EXE1:
set /p exe1 = EXE2:
set /p exe 2 = EXE3:
cd DIR
start exe
start exe1
start exe2
There you go!
To do it squentially:
call YouDriver.exe
call MeDriver.exe
call Mysoftware.exe
call will halt the batch file until program has closed.
Try and put it in the same directory of the files you want to run. If you can't, use cd C:\Directory\Name to change it to the directory where the MyDriver.exe file is. Then just do MyDriver.exe- you don't need a call or start statement.
MyDriver.exe
YouDriver.exe
MySoftware.exe
use cd at the start if you neeed to.

Resources