How do you have a program close when another one is closed? - windows

I'm trying to make it so that when I open a batch file, it will open 2 programs, and when the program Osu! closes, it will also close the program TabletDriverGUI The programs open, but I don't know how to close it.
Code:
start /d "C:\Users\ninja\AppData\Local\osu!" osu!.exe
start /min "TabletDriverGUI" "D:\EXE Files\Drivers\TabletDriverV0.2.3\TabletDriverV0.2.3\TabletDriverGUI.exe"
tasklist /V /FI "Osu! eq osu.exe*"
tasklist /V /FI "TabletDriverGUI eq TabletDriverGUI.exe*"
:loop
if "%ERRORLEVEL%"=="0" (
goto loop
) else (
taskkill /FI "TabletDriverGUI eq TabletDriverGUI.exe*"
)

Related

Start a program from Batch while preventing duplicates

I am starting a program from Batch, but if the program is already running, I want to avoid starting a second instance.
So far, I have crafted the following code, which seems to work:
TASKLIST /FI "IMAGENAME eq speedfan.exe" | FIND "speedfan.exe" >NUL
IF %ERRORLEVEL%==0 (
TASKKILL /F /IM speedfan.exe
)
START "" "C:\Program Files (x86)\SpeedFan\speedfan.exe"
Alternatively, if I want to keep the current instance instead of starting a new one:
TASKLIST /FI "IMAGENAME eq speedfan.exe" | FIND "speedfan.exe" >NUL
IF NOT %ERRORLEVEL%==0 (
START "" "C:\Program Files (x86)\SpeedFan\speedfan.exe"
)
Can this be improved/simplified?
Update: Thank you all for your comments! So far, my revised code is becoming:
TASKLIST /FI "IMAGENAME eq speedfan.exe" | FINDSTR /BLI "speedfan.exe " >NUL || (
START "" "C:\Program Files (x86)\SpeedFan\speedfan.exe"
)
(and maybe I'll drop the /FI "IMAGENAME eq speedfan.exe" part for the sake of simplicity)
Here's a quick example intended to request input confirmation to close it, if it is running, or to just open it if isn't running. I hope it is robust enough for your purposes.
#"%__AppDir__%tasklist.exe" /Fi "ImageName Eq speedfan.exe" /Fo CSV /NH^
| "%__AppDir__%find.exe" ":" > NUL && (
Start "" "C:\Program Files (x86)\SpeedFan\speedfan.exe") || (
"%__AppDir__%choice.exe" /M "Would you like to close SpeedFan"
If Not ErrorLevel 2 "%__AppDir__%taskkill.exe" /Im speedfan.exe /T > NUL)
I have split the long first line over two for tidiness, and because you don't particularly like long lines.

Batch script - close Cmd.exe window and restart

I have a batch file script which starts a program (bat) and after X seconds the batch script is close the program (exe) and restart it.
This is my code:
:loop
start "1.bat" "C:\1\1.bat"
Timeout 10
taskkill /f /im program.exe
goto loop
If the program is ending, I want that the Cmd.exe window closes, but only the "1.bat" window should be closed. I can't find a specified cmd window for my "1.bat" process in the task manager to close it.
If I close the programm.exe with && exit the batch doesn't restart!
You can do one of two things.
1 - If you have access to 1.bat (i.e. its not readonly or is not actually an .exe), then add exit after it runs the program.exe.
2 - Otherwise if you cannot modify 1.bat for whatever reason, you may need to add the following command line:
taskkill /FI "IMAGENAME eq cmd.exe" /FI "WINDOWTITLE eq 1.bat"
So your batch script will look as follows:
:loop
start "1.bat" "C:\1\1.bat"
Timeout 10
taskkill /f /im program.exe
taskkill /FI "IMAGENAME eq cmd.exe" /FI "WINDOWTITLE eq 1.bat"
goto loop
Or similar. Basically /FI is the filter switch and you can specify to filter based on the "IMAGENAME" (which equals "cmd.exe") and "WINDOWTITLE" (which equals "1.bat")

Batch File which checks for another Batch File process

so google didn't help me at all i need to ask here again.
I use this kind of method to check if my servers are running in 2 batch files.
tasklist /FI "IMAGENAME eq server_64.exe" 2> nul | find "server_64.exe" > nul
IF ERRORLEVEL == 1 (
echo Server is not running
echo.
) else (
echo Stopping Server ...
echo.
taskkill /F /IM server_64.exe > nul 2>&1
)
One to start and one to stop the servers.
Well this works great but when it comes to batch files it wont work for me...
I have one server which runs on phyton so start it via batch file.
My question is, is there a way to get somehow the batch file process status and stop it like it works for exe?
I hope i explained it good enough.
Thx in advance! :)
You can try it with a batch file like this :
#echo off
set "Process=server_64.exe"
Title Checking for status of this process ===^> "%Process%"
tasklist /nh /fi "imagename eq %Process%" 2>nul |find /i "%Process%" >nul
IF '%ERRORLEVEL%' EQU '1' (
Color 0B
echo.
echo "%Process%" is not running
) else (
Color 0C
echo.
echo Stopping "%Process%" ...
taskkill /F /IM "%Process%" > nul 2>&1
)
pause
Omg i found the solution, this was a beast...
tasklist /fi "imagename eq cmd.exe" /v /fo table /nh | find /i "Broker" 2>nul
but what is starmnge is that i cant get the output to be silnce...
when i try to mute it it gives me always error level 1.
tasklist /fi "imagename eq cmd.exe" /v /fo table /nh 2>nul | find /i "Broker" 2>nul
so whats wrong with this? ^

Tasklist doesn't have access to a certain process

I made a batch file with the following script:
#echo off
goto start
:start
"C:\Games\SV_Debug\Data\Win32\InvisibleM2.vbs"
goto check
:check
tasklist /FI "IMAGENAME eq Launcher2.exe" >swap | find /I /N "Launcher2.exe" >swap
if "%ERRORLEVEL%" == "0" (
goto waitrecheck
) else "%ERRORLEVEL%" (
goto start
)
:waitrecheck
ping 127.0.0.1 -n 3
goto check
This batch file is executed by a vbscript that make's this batch invisible.
The batch file runs a vbscript that runs Launcher2.exe normal and not invisible.
But this batch above here can't check is Launcher2.exe is running.
When it tries I get this error:
The process does not have access to the file because it's being used by another program.
How do I fix this?
tasklist /FI "IMAGENAME eq Launcher2.exe" >swap | find /I /N "Launcher2.exe" >swap
You can not send the output of two programs to the same file at the same time
tasklist /FI "IMAGENAME eq Launcher2.exe" | find /I /N "Launcher2.exe" >nul

how to get PID from command line filtered by username and imagename

I need to be able to get the PID from a running process (cmd.exe) using the command line.
The problem is there are two cmd.exe running. One is under the username SYSTEM and one is compUser. Is there a way that I can grab the PID of the compUser cmd.exe?
Edit: This needs further explanation.
I'm doing this from a batch file. One of the calls that I'm making in my batch file starts a cmd.exe that never dies. So killing that cmd.exe would be simple:
taskkill /F /IM cmd.exe /FI "username eq compUser"
The problem is that the batch file that I'm in is being handled by another instance of cmd.exe under the username compUser. What I'm attempting to do is get the PID from the original cmd.exe before I start the second cmd.exe. That way I can just use the command:
taskkill /F /IM cmd.exe /FI "username eq compUser" /FI "PID neq [orignal task's PID]"
The way I ended up having to do this was use:
TASKLIST /NH /FI "IMAGENAME eq cmd.exe" /FI "username eq compUser"> psid.txt
FOR /F "tokens=2" %%I in (psid.txt ) DO set pIdNotToKill=%%I
right before I started the batch script that hangs. Then when I was ready to kill the hanging cmd window:
taskkill /F /IM cmd.exe /FI "PID ne %pIdNotToKill%" /FI "username eq compUser"
There is probably a better way, but this worked.
This will display all processes named "cmd.exe" for the user "compUser":
tasklist /FI "IMAGENAME eq cmd.exe" /FI "USERNAME eq compUser"
I set the Title to my own cmd window, other cmd windows what I call or run by "start /b" I rename to other window title name. Next I detect PID, set MYPID and kill all other cmd with OTHER PID of my.
All on one file, working in other label loop with timeout dalay.
#(#Title TitleOfMyScript)
for /f "tokens=2" %%a in ('tasklist /fi "imagename eq cmd.exe" /fi "windowtitle eq TitleOfMyScript" /nh') do set MYPID=%%a
taskkill /FI "PID ne %MYPID%" /FI "IMAGENAME eq cmd.exe" /F
I'd like just to share my piece of code, maybe it will be useful for somebody. It is based on the comment from jiggawagga, but it can terminate many processes:
#ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION
REM kill old server process
SET pIdNotToKill=
SET pIdFilter=
FOR /F "tokens=2" %%I IN (%SERVER_HOME%\psid.txt) DO (
IF NOT "%%I"=="No" (
SET pIdNotToKill=!pIdNotToKill! %%I
SET pIdFilter=!pIdFilter! /FI "PID ne %%I"
)
)
IF NOT "!pIdNotToKill!"=="" (
ECHO Killing all Java processes except%pIdNotToKill%
ECHO TASKKILL command will be called with the filter%pIdFilter%
TASKKILL /F /IM java.exe %pIdFilter%
)
DEL %SERVER_HOME%\psid.txt
TASKLIST /NH /FI "IMAGENAME eq java.exe" > %SERVER_HOME%\psid.txt
right before I start server process.

Resources