Start firefox and close after few minutes with batch file - batch-file

i have a small script that starts waits 60 seconds,starts firefox and runs a imacros script
but the problem is i want to close firefox after 60 seconds and start it again through loop.But what this script does is .It waits for me to close firefox.Then starts 60 seconds timer and then loops the script.
What i want to do is .It should not wait for me to close firefox and directly force closes firefox after 60 seconds and runs in loop
#Echo off
:loop
cls
taskkill /f /im Firefox.exe
cls
taskkill /f /im crashreporter.exe
cls
timeout /T 60
start ""
"C:\Program Files (x86)\Mozilla Firefox\firefox.exe" imacros://run/?m=l4lyt.iim
timeout /T 60
goto loop
What am i doing wrong?why does it waits for me to close firefox instead of directly going to next line.
i.e
timeout /T 60

You had the start command on a separate line from firefox.exe, therefore it launched a command prompt window and then called firefox.exe instead of executing it.
Additionally you don't need to use cls to clear the screen, instead use >nul 2>&1 to suppress output from the taskkill command(s)
Updated script:
#echo off
start "" "C:\Program Files (x86)\Mozilla Firefox\firefox.exe" imacros://run/?m=l4lyt.iim
timeout /T 60
taskkill /f /t /im Firefox.exe /im crashreporter.exe >nul 2>&1
%0

Related

Restart Batch Script

I am trying to have a FiveM server auto-restart. Currently, this is how you startup the server
echo on
title FiveM
cd %~dp0
:loop
%~dp0run.cmd +exec server.cfg %*
timeout /t 6 >null
taskkill /f /im FiveM >nul
timeout /t 30 >null
goto loop```
pause
I want my server to auto restart after, lets say, 1 hour. To stop the script normally you need to cntrl+c.
How do I go about this?

Open and close websites in batch

I'm trying to make an batch file that opens a website for 5 seconds, then closes it an then loop the batch, I got not much Succes, does anyone now how to do this, without letting all te cmd pages on screen(so the batch also closes the cmd after running) I hope someone can help me.
this is the code i got so far, it works but it doesnt close the cmd.exe (Web is the name of the .bat file)
#echo off
start "chrome.exe" "http://www.websitehere. co"
timeout /t 3
taskkill /IM chrome.exe /F
:loop
start /wait Web
goto loop
Greetz Bas
I've tried this code, hope it's good for you:
What i've done is do a while loop, for you to choose how many times will the loop run.
I set the timeout as 10 seconds but you can change it for 5 seconds.
#echo off
set /A x=0
:while
if %x% LEQ 2 (
echo %x%
start /max chrome.exe https://www.google.es
timeout 10
taskkill /F /IM chrome.exe > null
set /A x=%x%+1
goto :while
)

Script to restart a program every few minues

I've tried restarting a program every few minutes with a batch file which looks like the following. However it only opens the .exe a lot of times which causes them to crash. Anyone knows why this problem occurs?
#echo off
:loop
start "programm" "D:\Downloads\programm.exe"
timeout /t 1200 >null
taskkill /f /im "programm" >null
timeout /t 7 >null
goto loop
I hate a short answer, but it's an easy and quick fix. null is nothing, use nul as it's almost certainly skipping the invalid output name.
So the code:
#echo off
:loop
start "programm" "D:\Downloads\programm.exe"
timeout /t 1200 >nul
taskkill /f /im "programm" >nul
timeout /t 7 >nul
goto :loop
taskkill /f /im "programm" >null
Remove the >null and look why it doesn't kill the program.

Auto restarting a program with batch

My current program runs its tasks, then closes itself, the batch file then restarts it after 60 seconds. The problem is that sometimes the program is unable to close itself, and everything is stuck.
I need to modify the script, so it will autorestart after 5 minutes, if the program does not close itself.
:launch
START /wait program.exe
rem delay 60 seconds
ping 127.0.0.1 -n 60 > nul
GOTO :launch
You can do something like that :
For example you check for every 5 minutes if the calc.exe process is running or not.
#echo off
Mode con cols=55 lines=3
:CheckRunningProcess
Cls
echo(
Set "MyProcess=calc.exe"
Title Check for Running Process "%MyProcess%"
tasklist /NH /FI "imagename eq %MyProcess%" 2>nul |find /i "%MyProcess%" >nul
If not errorlevel 1 ( Color 0A & Echo "%MyProcess%" is running
) else (Color 0C & echo "%MyProcess%" is not running, so we start it right now & start "" "%MyProcess%")
TimeOut /T 300 /NoBreak >nul
Goto :CheckRunningProcess

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