I want to run and kill a process with same batch file - batch-file

I have bitlord I want this program to run when i click a batch file and when i don't want this program to run i want to kill it with same batch file. Can anyone please write the script Thanks a lot for giving your time.
I tried it scripting but failed.

If bitlord is not running then this will launch it
If bitlord is running then this will kill it
Change line 2 and line 3 to set the program name and path.
#echo off
set "program=bitlord.exe"
set "folder=c:\program files\bitlord"
tasklist |find /i "%program%" >nul || start "" "%folder%\%program%" & goto :EOF
tasklist |find /i "%program%" >nul && taskkill /f /im "%program%" & goto :EOF

Okay. I think i understand your question now:
#echo off
taskkill.exe /f /im notepad.exe
start calc.exe
The magic is to use the start command which starts a new command asynchronous. Meaning, it doesn't wait for the termination.

Related

Bat script to run commands in a running cmd

I use a windows .bat script to start all of my game servers
The script looks like this:
cd "C:\Users\Administrator\Desktop"
start Minecraft_HUB
start Minecraft_LOBBY
start Minecraft_SURVIVAL
with many more servers
It works for launching all of them at the same time but I need to manually type stop / exit in every single one to shut them down.
Is there any way I could have a .bat script that would insert and run a save & shut down command in a running cmd console?
I don't yet know how to make that script
From the prompt:
for /f "tokens=1,2delims=," %b in ('tasklist /fo csv') do echo %~b|find /i "minecraft">nul&if not errorlevel 1 echo taskkill /pid %~c
As a batch-file line:
for /f "tokens=1,2delims=," %%b in ('tasklist /fo csv') do echo %%~b|find /i "minecraft">nul&if not errorlevel 1 echo taskkill /pid %%~c
Note that the taskkill command will merely be echoed to the console. Remove the echo before the taskkill to actually execute the taskkill
Untested. Use at your own risk.

Turning Off a Program When the other one closes .Bat file

This is my code so far ive tested it and both programs open when .bat is run , What im after is the code to close program 2 when program 1 closes any help would be appriciated as i have no coding knowledge :(
#echo off
start "TestFolder" "C:\Program Files (x86)\testfolder\test1.exe" %1
start "Testfolder2" "C:\Program Files (x86)\testfolder2\test2.exe"
taskkill /f /im test2.exe
exit
You need to setup a FOR /F Loop to monitor the output of TaskList to check for the window title of the 2nd program (and you'll need to explicitly set the window title of that program to something unique to do that.)
Then you will need to use Taskkill to kill the other task.
#(SETLOCAL
echo off )
CALL :Main
( ENDLOCAL
EXIT /B )
:Main
start "TestFolder1" "C:\Program Files (x86)\testfolder\test1.exe" %1
start "Testfolder2" "C:\Program Files (x86)\testfolder2\test2.exe"
CALL :Loop
taskkill /f /im test2.exe
GOTO :EOF
:loop
SET /A "n=10"
TIMEOUT 10
tasklist /V /NH /FI "IMAGENAME eq test1.exe" | FIND /I "TestFolder1" &&(
GOTO :loop
)
GOTO :EOF

Dealing with Redirection Operator with User Input

To get a complete Log Output of a particular Batch Script, I have been using the 2>&1, but in some of my batch script need to have the user input also (i.e.- yes/no), in that case how could I have the complete Log Output of the specific Batch Job?
NB : Next time provide us your code to get more helped !
Try Something like that :
#echo off
set /p var=prompt text
echo %var% >log.txt 2>&1
set process=Test.exe,test2.exe,calc.exe,skype.exe
For %%a in (%process%) Do Call :KillMyProcess %%a
pause
Exit /b
:KillMyProcess
Taskkill /IM "%~1" /F >> log.txt 2>&1

BAT commands to recognize if process is running and display prompt

I need to create a bat that recognizes if a process is running, and if the process is running I need to add a prompt asking if the user wants to end the process to continue with some other tasks.
I have the command to recognize if a process is running(this is the one that worked for me):
#echo off
tasklist /nh /fi "imagename eq vmware-view.exe" | find /i "vmware-view.exe" >nul && (
echo VMWare Horizon is running
) || (
echo VMWare Horizon is not running
)
pause>nul
and I have the command to prompt for an imput:
#echo off
setlocal
:PROMPT
SET /P AREYOUSURE=Are you sure (Y/[N])?
IF /I "%AREYOUSURE%" NEQ "Y" GOTO END
But I don't know how to put those 2 together to say that, if the process is running, display prompt asking user if bat can end process to continue with an update, and if not running just go on with update.
could you please assist?
thanks in advance
I remember from the old DOS days that you could use GOTOs.
for instance a bat file containing the following:
#ECHO OFF
ECHO something
GOTO LABEL0
ECHO something else
GOTO LABEL1
:LABEL0
ECHO some label
:LABEL1
ECHO some other label
will print:
something
some label
some othe label
you will not see
something else
because the first GOTO skipped to LABEL0
You can use a label like :QUIT or :END at the end of your bat and skip at the end if you have nothing to do.
Use ERRORLEVEL to see if TASKLIST and FIND got the process. Afterwards use CHOICE.
#echo off
set task=vmware-view.exe
tasklist | find /i "%task%" > nul
if %errorlevel%==0 (
echo task is running.
choice /c yn /t 10 /d n /m "should we kill it?"
if errorlevel 2 goto end
taskkill /f /im "%task%"
) else (
echo task is not running.
)
:update
echo updating...
:end

How to Kill a Command after certain period of time in Command Prompt even if it is running state

I am running a command in command prompt and I want to kill it after 15 minutes. I don't care if it is completed or not. I don't want to have any human interaction to kill the running command such as someone has to press CTRL+C to kill it.
Is there a way to do it.
Please note I don;t want to use any third party tools or scripts.
start forfiles /s
timeout /t 5
Taskkill /im forfiles.exe /f
is one way.
Did you mean something like that ?
#echo off
Tracert www.google.com
timeout /t 900 /nobreak >NUL
Taskkill /im cmd.exe /f
Edit : based on the blueray's comment :
how can I put this command in a single line to use in CMD?
Tracert www.google.com & timeout /t 900 /nobreak >NUL & Taskkill /im cmd.exe /f
May be this could help
start your-command-here
ping 127.0.0.1 -n 120 > NUL
taskkill /im cmd.exe /f
Explanation:
start: this command starts executing your command
ping: it pings your local machine(ip : 127.0.0.1) for 120 sec and >NUL redirects the output to nowhere else the output of ping command will be displayed on the cmd screen
taskkill: it is used to kill any task
/im: image name of the process to be terminated. if the command is running on cmd then cmd.exe or any program that you need to kill.
Hope it helps.
Try this, works well for me:
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
#echo off
set _time=0
set _timeout=30
start myprocess.exe
:waitforprocess
set /A _time=_time+1
IF !_time! GTR !_timeout! goto TimedOut
rem TaskList will return the task list of the image as specific
rem or it will return INFO: No tasks are running.... so look for
rem the INFO statement using FindStr. FindStr will return a errorlevel
rem of 0 if it found the string and a 1 if it did not, so use that
rem to work out next steps.
rem -------------------------------------------------------------------
tasklist /NH /FI "IMAGENAME EQ myprocess.exe" | findstr INFO
rem ERRORLEVEL 1 = Did not find the string, so try again.
IF ERRORLEVEL 1 (
timeout /T 1 /NOBREAK > nul
goto :waitforprocess
)
GOTO DONE
:TimedOut
ECHO We timedout so will kill the process
taskkill /FI "IMAGENAME EQ myprocess.exe" /T /F
:Done
echo finished

Resources