I am creating a batch file program where if you open a mspaint.exe, if its idle for 10 seconds the program will close automatically. I have created this:
#echo off
:Start
tasklist /FI "IMAGENAME eq mspaint.exe" | findstr "mspaint.exe" >nul
IF "%ERRORLEVEL%" == "0" GOTO Running
IF "%ERRORLEVEL%" == "1" GOTO NotRunning
:Running
***::Check if idle***
GOTO Terminate
:NotRunning
GOTO EOF
:Terminate
timeout 5
taskkill /im mspaint.exe /f
ECHO Paint has been terminated due to inactivity
PAUSE
:EOF
EXIT
I have trouble finding the idle syntax(seems it doesn't exist), is there another way to make the idle time work? And since this is my first time creating a batch file, I really need a helping hand here.
Not the answer you want to hear, but the one that you need. The only way to monitor another process for activity is to install a window hook, but that's going to require a much lower level language (C or C++).
If that doesn't put you off then start with the Microsoft Hooks Overview and post further questions. Good luck (you'll probably need it).
Thanks for the help but that seems complicated. I used SCHTASKS instead and it worked :)
Related
I am looking to exit my loop at a defined number of loops (10 - Loops). I looked at a few things on Google, but the loop was to do something else, so I was a little lost.
Here is my basic loop script and I hope one of you can educate me and point me in the right direction. If your wondering what I am doing it this way, here's why and if you have a better option, please let me know. I am fairly new to this batch scripting and I am open to suggestions.
This Uninstaller.exe does not pause the batch script. So I am basically looking for the Uninstaller.exe, which will be deleted upon completion of the Uninstaller.exe process. So once the loop detects the deletion of the executable, it will then exit the loop and move on to the next action in the script.
I have had a couple times where the uninstaller.exe crashed before it ended and deleted its self and prevented the batch file from continuing on. So I figured it would be best to only have it loop for a set number of times before exiting.
:: Uninstall App
"C:\Program Files\App Name\uninstall.exe" -quiet
::Validates the uninstall
SET LookForUninstaller="C:\Program Files\App Name\uninstall.exe"
:CheckForUninstaller
IF NOT EXIST %LookForUninstaller% GOTO ExitLoop
TIMEOUT /T 5 >nul
GOTO CheckForUninstaller
:ExitLoop
You may benefit from using the "start" command to run the uninstaller on a different process.
And you may also take advantage of its optional "/WAIT" which will allow the batch to wait until the process is finished to continue.
You may not need the loop after all.
i.e.
start /WAIT "C:\Program Files\App Name\uninstall.exe"
Here is what I did and seems to work ok.
::Set Uninstaller Variable
SET AppToUninstall="C:\Program Files\App Name\uninstall.exe"
:: Uninstall App
"%AppToUninstall%" -quiet
::Loops for 12 times in 10 second intervals (Total 120 seconds) to confirm deletion. Loop will exit after 12 loops and move on if uninstaller is not deleted.
for /l %%i in (1,1,12) do (
TIMEOUT /T 10 >nul
IF NOT EXIST %AppToUninstall% GOTO ExitLoop
)
:ExitLoop
sorry if I am using wrong terms, I am new to this stuff. so I have been trying to get this command (taskkill /f /t /im (the process)) to keep repeating because the process that I am trying to kill keeps coming back. so I thought if i can get the command to keep repeating it will keep the process closed. please I would like some help, thank you.
Just loop through
#echo off
:loop
taskkill /f /t /im (the process)
goto loop
Or add a delay using ping or timeout
#echo off
:loop
taskkill /f /t /im (the process)
timeout /t 30 /nobreak >nul rem Change out 30 for the number of seconds needed in the delay
goto loop
As #MrLister said in the comments above, address the problem directly. Ask yourself "did i get this program from a reliable source?" if not, it could be malicious. If it is safe, then why is this behavior occurring? Could it be scheduled with taskscheduler? Best of luck.
Sorry for my bad english expression ... i'm not native.
I have searched for an answer for a while ... maybe i'm not doing this right but it seems that my (simple) code is not working for a reason.
As soon as I open the batch programm it shuts without me having the time to read the error message :(
could some one please correct this one ??
I am grateful for any kind of help !
#echo off
color 0a
mode 1000
title THE OFFSWITCH
:start
cls
if %time%== "12:00:00.00" goto :time
echo.
echo IT IS NOT TIME YET : %time%
goto start
:time
echo hello
pause>nul
It is pure luck, if your code works. In the most cases, it will just miss the time, because the loop is not fast enough to get the correct centisecond.
Instead of
if %time%== "12:00:00.00" goto :time
you should use
if "%time%" geq "12:00:00.00" goto :time
(if time is "greater or equal" than...)
Also you should consider to put a delay into the loop, because it eats all of the CPU-Time, it can get, and thus slows down your system. For example timeout /t 1>nul waits for one second.
I'm using a network management tool to apply updates to software and I have an issue where if a users is already using the program you want to update the update will fail as you would expect.
I have been trying to put together a batch script that will detect whether the the program is running and if it is the script will wait until the user closes down the program and the apply the msi update.
I've pretty much scoured google but can only really find previous scripts that kill the program first before proceeding and I don't want that to happen as the user may lose work.
Hope someone can help!
#echo off
start "" notepad.exe
:loop
(tasklist | find /i "notepad.exe" && ( ping -n 2 localhost & goto loop)) >nul
echo Notepad closed
This just starts notepad.exe (the running program) and waits until it is closed. Adapt according to your needs
`tasklist |find "programname"`
will tell you if the programm is running:
if %errorlevel%==0 echo running
put just a little loop around it:
:loop
tasklist |find "programname" >nul
if %errorlevel%==0 (
echo prgram running
timeout 2>nul
goto loop
)
echo program not running
I would like to start a program with a windows batch file. But the program should stop after a certain timeout value. For example: Run the program 60 seconds and stop it after 60 seconds.
Under Linux, there is this nice timeout command to do what I want. Windows has also a timeout command, but its just to pause a command, to delay its execution. Is there something else under Windows to do so ?
Setup: Windows 7, 64 Bit, Professional
start yourprogram.exe
timeout /t 60
taskkill /im yourprogram.exe /f
Bali C gave a concise and to the point answer.
I needed something a little more featureful and reusable.
Based on Bali C's Example. I came up with this.
If anyone should need the same as me.
your.bat
REM...
CALL STARTwaitKILL..bat /relative/path/your_program.exe
REM...
STARTwaitKILL.BAT
#ECHO OFF
IF[%1]==[] GOTO EOF
IF NOT EXIST %1 GOTO EOF
REM SET PRIORITY=/NORMAL
REM ADJUST PRIORITY, BELOWNORMAL LETS BATCH FILE RUN MORE SMOOTHLY
REM WITH A PROGRAM THAT CONSUMES MORE CPU. SEE ABOUT MAXWAIT BELLOW
SET PRIORITY=/BELOWNORMAL
REM SET PRIORITY=/LOW
REM 0 NORMAL WINDOW :: 1 NO WINDOW :: 2 MINIMIZED WINDOW
SET /A HIDDEN=1
REM MAXWAIT HERE IS MORE LIKE MINIMUM WAIT IN WINDOWS.
SET MAXWAIT=10
SET WAITCOUNT=0
SET ARGS=/I %PRIORITY%
IF %HIDDEN% EQU 1 SET ARGS=%ARGS% /B
IF %HIDDEN% EQU 2 SET ARGS=%ARGS% /MIN
START %ARGS% %1
:WAIT
IF %WAITCOUNT% GEQ %MAXWAIT% GOTO KILL_IT
TIMEOUT /T 1 > NUL
SET /A WAITCOUNT+=1
FOR /F "delims=" %%a IN ('TASKLIST ^| FIND /C "%~nx1"') DO IF %%a EQU 0 GOTO RUN_DONE
GOTO WAIT
:KILL_IT
TASKKILL /IM %~nx1 /F > NUL
:RUN_DONE
Could be fleshed out ore to take more arguments for priority and such, but I don't have the need for it. Shouldn't be hard to add.
Don't exist any command in Windows to delay an app or to set a timeout for an app
Timeout in Windows is for Delay the execution process of CMD/Batfile, nothing more utility.
You can use external tools for that, I don't remember the name of any now, so many underground software, sorry, but I remember that in the autoit official forum exists a similar commandline tool to launch an app setting the timeout,
and maybe in the tool NIRCMD, or ps2exec, check their help files, or someone inside the WAIK Kits.
This is the only you can do:
#Echo OFF
:: First launch the app in background mode, because some applications stops the execution of CMD.
Start /B ".\Dir\Your app.exe"
:: Then stay in background for a certain time
Timeout /T "Seconds"
:: Continue your code...
Pause&Exit
The start+timeout+taskkill waits exactly the given time. Since I needed to stop waiting if the process exits earlier, I created my own solution in C++.
The tuxliketimeout program mimics the GNU timeout. Feel free to download&compile from
https://github.com/cernoch/tuxliketimeout
In windows 10 the easiest way is with scriptrunner:
Demonstrate the timeout by running a pause command (this will kill the called process):
ScriptRunner.exe -appvscript cmd "/c" "pause" -appvscriptrunnerparameters -wait -timeout=20