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.
Related
Used a lot of time to se if i could figure this out but no luck.
My issue currently is via CMD i can do
taskkill /F /FI "WindowTitle eq Administrator: Server2" /T
But i add this to a bat file, it removs the extra space between Administrator: Server2 and for some reason that wont work ofc.
The thing i wanna do is being able to close down a specific cmd with a bat script, since that CMD runs a game server, and i dont want to target them all ad once.
Is there any way to only close the specific CMD?
Since "WMIC process" doesn't include "windowtitle", I reverted back 'tasklist' and 'findstr /l /c:"your string goes here"'.
The "/l" and "/c" together allow you to specify extra spaces. Although I don't have an example on my system with the same number of spaces your example has, I recall using for something similar in the past. Please try this and let me know:
Cmd
for /f "tokens=2" %A in ('tasklist /v /fi "imagename eq chrome.exe" ^|findstr /l /c:"Administrator: Server2"') do #echo taskkill /pid %A
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
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.
Basically I have a simple batch script just now that starts a program multiple times let's call it 1.exe, 1.exe will launch 20 times and then be killed after % amount of seconds (specified by a command line argument).
What I need done is if 1.exe is already running, launch 2.exe instead, if 2.exe and 1.exe are already running launch 3.exe instead and so on.
All the code I really have just now is
Timeout /t 20 /nobreak >nul
echo.
taskkill /F /IM %programname%
echo.
pause >nul
and also the launching of the 1.exe
Any help is appreciated.
The Batch code below launch 2.exe if 1.exe is already running, launch 3.exe if 2.exe is already running and so on:
for /L %%i in (1,1,20) do (
for /F "skip=3" %%e in ('tasklist /fi "imagename eq %%i.exe"') do (
if "%%e" equ "%%i.exe" set lastExe=%%i
)
)
set /A lastExe+=1
%lastExe%.exe
I'm trying to check what the status is on my XenApp servers for the spoolsv.exe process. I've got the command down to run individually from my XP workstation, but can't seem to get it to iterate through a text file. Here's what I have so far, what will make this populate Servers X-XX on my CMD screen?
#echo off
FOR /F "usebackq" %%G IN ("C:\Documents and Settings\userid\Desktop\Scripts\servers.txt") DO echo tasklist /S %%G /u domain\userid | find "spoolsv.exe"
pause
I can't seem to get it to run correctly, and sometimes it will just pop up my servers.txt file in notepad and not even run. What am I missing?
As you have it presented, tasklist never runs. The "do echo tasklist..." snippet means the literal string "tasklist /S server-one..." is being echo'ed to stdout. Since none of these literal strings contain "spoolsv.exe", the "find" command won't match anything.
Try the following instead:
#echo off
FOR /F "usebackq" %%G IN ("C:\Documents and Settings\userid\Desktop\Scripts\servers.txt") DO call :RunTasklistForOneServer %%G
pause
goto :EOF
:RunTasklistForOneServer
set ServerName=%1
echo Calling server %ServerName%
tasklist /S %ServerName% /u domain\userid | find "spoolsv.exe"