Killing a Batch script from another - batch-file

I have a script that will call other scripts to run then go back to process.
Example:
Call a script to ping and when it completes, go back to script.
My issue is I have a script called Bounce.bat
Here is the code:
#Echo off
set IPADDRESS=127.0.0.1
set INTERVAL=60
:PINGINTERVAL
ping %IPADDRESS% -n 1
timeout %INTERVAL%
GOTO PINGINTERVAL
What I need is the command that will kill the Bounce.bat from inside another batch script.
I was going to try killing CMD but that kills the original script as well.
I Have tried experimenting as well as looking at script examples.
Does anyone know what is needed to kill this bat only?

You can use the Command "taskkill" to stop other processes in Windows.
You can write something like this in your other batch-file:
taskkill /IM MYBATCHFILENAME.bat
You can force to kill the process, if you are using the parameter /f at the end of your command.

Related

Unable to launch multiple programs via a batch file

Am trying to create a batch file, that would launch multiple programs. But unfortunately, things don't seem to work.
Kindly, find below my requirement:
Open InfluxDB server
Launch Grafana application.
Commands used in the batch:
#echo off
cd "C:\Users\C51539A\Downloads\influxdb-1.5.2-1"
Start.cmd
timeout 5
cd "C:\Users\C51539A\Downloads\grafana-5.1.3\bin"
grafana-server.exe
The above script, launches InfluxDB. But doesn't moves further.
Could you please suggest me, on how to proceed?
You need to use the call keyword to have control returned to the caller after invoking another batch script:
#echo off
cd "C:\Users\C51539A\Downloads\influxdb-1.5.2-1"
call start.cmd
...
Should start.cmd run InfluxDB synchronously (i.e. not in the background) you need to launch it in a separate window:
#echo off
cd "C:\Users\C51539A\Downloads\influxdb-1.5.2-1"
start "InfluxDB" cmd /c start.cmd
...
#echo off
cd "C:\Users\C51539A\Downloads\influxdb-1.5.2-1"
start InfluxDB
ping -n 6 127.0.0.1 > nul
cd "C:\Users\C51539A\Downloads\grafana-5.1.3\bin"
start grafana-server
Edit the "start InfluxDB" and "start grafana-server" to be the correct exe names, without .exe

Running files in the current window with batch

This is pretty hard to explain so I'll do my best.
When I use the start command in Batch, It'll create a new window etc.
I'm trying to make my own console batch script that isn't as annoying as cmd.exe.
Problem is running commands like help, echo and so on in the current window.
My current code is
:console
set COMMAND=n
set /p COMMAND=%~dp0console.exe^> %=%
if %COMMAND%==exit exit
start %COMMAND%
rem Wait 1000
goto console
I'm using it with Batch to EXE Converter so rem Wait 1000 is valid.
The big problem with the script is that if a user types help or echo and any other commands you can run normally in cmd.exe, it'll open a new cmd.exe window and run the script. I do not want this to happen, is there a way to solve the problem?
Things I've tried:
Instead of using start %COMMAND% I just used %COMMAND%. This
resulted in an error.
(Will add on this list when things that don't work are suggested)
There are two options:
call %command%
Which will wait until the command is over
start /b %command%
Which will start the command in the current window

How to kill a service in a batch file every 20 seconds?

A task called "FireSvc.exe" (McAffee service) keeps interfering with our app upgrades. I put
> taskkill /f /im "FireSvc.exe"
in a batch file. We continuously run this during installs so the software will successfully upgrade. I'm not positive why this service starts back so often. We have to run from the command line, because in the task manager you get "access denied" when trying to kill this task.
My question is, how would you make this run every 20-30 seconds?
We cannot install any type of non-approved software either. So, theres that...
Thanks for any input.
Here's what we use:
:runagain
taskkill /f /im "FireSvc.exe"
timeout /T 5
goto runagain
You can use vbscript's sleep command in a batch file with cscript like so...
First create a vbscript file (.vbs extension) that contains the following code (don't forget to save it with ANSI encoding otherwise it won't work):
Wscript.sleep 2500
Wscript.exit
Create a batch file in the same directory with this code:
#echo off
:Kill
cscript //NoLogo file.vbs
taskkill /f /im "FireSvc.exe"
GoTo Kill
I currently don't have access to a PC to check if it works so let me know what happens. I still think there might be a cleverer alternative... cheers!
Edit: Btw you can also simulate a sleep command with the ping command like so:
ping localhost -n 1 -w 2500 >nul
And if you are using windows vista or above you can use the timeout command.

start executable in scripts with output redirect

I have a batch script that I use to start and stop a service. The whole script is encapsulated in a subroutine so that i can do output redirect:
call :sub >> C:\Users\Administrator\Desktop\output%LogFilename%.txt
#exit /b 0
:sub
.
start cgsservice.exe -c config\cgs.json
.
The problem that I am having is that after starting the service using the script I CANT run the script again to stop the service. I get the following error:
The process cannot access the file because it is being used by another process.
I know what the issue is but I dont know how to fix it. The issue is the redirect to the log file, since the service is running it wont let me run the script again because it cant access the output%LogFilename%.txt file. Ones I kill the service manually im able to run the script again. Any ideas how to solve this issue with out getting rid of the redirect
You won't be able to "reclaim" the log file while the process is running. Instead you will have to stop the process which has a lock on it. You can automate the stop by adding this line prior to your call to :sub:
REM Stop any running instances of the service.
REM This is needed to release the lock on the log file.
TaskKill /im cgsservice.exe /f
Note this is the same as forcefully terminating the application, so I'm not sure what ramifications it may have. You can try without the /f switch (which will attempt a graceful closing) to see if the process stops this way.
Your other option would be to just have a separate log file for the cgsservice.exe process, but I'm not sure how that would fit into your workflow. To implement this, update the line:
call :sub >> C:\Users\Administrator\Desktop\output%LogFilename%_cgsservice.txt
Note that doing this will cause a lock on the alternate log file until the cgsservice.exe process is terminated. You can use the same TaskKill statement as above to stop it.
If you want to check if cgsservice.exe is not started and then start it, this should do it:
FOR /F "usebackq tokens=* delims=" %%A IN (`TaskList /FI "IMAGENAME eq cgsservice.exe"`) DO (
REM Check against the result of TaskList when no matches are found.
IF "%%A"=="INFO: No tasks are running which match the specified criteria." (
REM Service is not started. Start it now.
START cgsservice.exe -c config\cgs.json
)
)

DOS batch command

I wonder if someone can help me in DOS batch command script.
The script will run two applications i.e. app1.exe, and app2.exe. First app1.exe will run and once app1.exe has finished running then the app2.exe will run.
(Please know app1.exe must finish running before app2.exe will start)
Regards.
Use "call"
call app1.exe
call app2.exe
You can force a sequence of programs by using
#echo off
start /WAIT /B app1.exe
start /WAIT /B app2.exe
/B means that the new app will be started without creating a new window.

Resources