FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Run Firefox every 5 Min from Windows Batch File

This Windows Batch Script will run firefox every 5 minutes.

@ECHO OFF&& SETLOCAL&& PUSHD "%~dp0"&& SETLOCAL ENABLEDELAYEDEXPANSION&& SETLOCAL ENABLEEXTENSIONS&& SET V=5&& IF NOT "!V!"=="5" (ECHO DelayedExpansion Failed&& GOTO :EOF)

REM ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
REM ++                                                                                                                                                                                                ++
REM ++   SCRIPT LOCAL VARIABLES                                                                                                                                                                       ++
REM ++                                                                                                                                                                                                ++
REM ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

SET _FIREFOXBIN=C:Program FilesMozilla Firefoxfirefox.exe
SET PATH=%ProgramFiles%;%PATH%
SET _PLOG=L:PPPatlog.log

REM ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
REM ++                                                                                                                                                                                                ++
REM ++   MAIN PROGRAM EXECUTION                                                                                                                                                                       ++
REM ++                                                                                                                                                                                                ++
REM ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

REM CREATE AT JOB TO RUN THIS SCRIPT EVERY 5 MINUTES
CALL :CREATEATJOB

CALL :STARTFIREFOX

:: CHECK ONLY 1 FIREFOX IS RUNNING
CALL :FIREFOXCHECKMULTI

POPD && ENDLOCAL && EXIT && EXIT
GOTO :EOF

REM ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
REM ++                                                                                                                                                                                                ++
REM ++   SCRIPT FUNCTIONS                                                                                                                                                                             ++
REM ++                                                                                                                                                                                                ++
REM ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

REM =========================================================================================================================================================================
REM =   STARTFIREFOX
REM =========================================================================================================================================================================
:STARTFIREFOX
ECHO.&& ECHO STARTING FIREFOX
CALL :KILLDUPES "firefox.exe"
START "firefox" /HIGH /MIN  "%_FIREFOXBIN%"
IF ERRORLEVEL 1 ( CALL :SPEAK "FAILED. START FIREFOX" ) ELSE ( ECHO OK )
EXIT /B

REM =======================================================================================
REM =   CREATEATJOB - runs job (START this file) every 5 minutes
REM =======================================================================================
:CREATEATJOB
ECHO.&& ECHO CREATE AT JOB
AT | FOR /F "tokens=1" %%i IN ('FIND /I "%~f0"') DO AT %%i /delete /yes >NUL 2>&1
SET /A H=!TIME:~0,2!&& SET M=!TIME:~3,2!
SET Y=%H%:%M%&& SET /A M+=5
IF !M! GEQ 60 ( SET /A H+=1&&SET /A M-=60 )
IF !H! GEQ 24 SET /A H-=24
SET M=0!M!&& SET H=0!H!
AT %H:~-2%:%M:~-2% /INTERACTIVE %ComSpec% /E:ON /D /Q /C START /MIN %ComSpec% /E:ON /D /Q /C "%~f0" >NUL 2>&1
ECHO OK
EXIT /B

REM =======================================================================================
REM =   KILLDUPES - kills duplicate processes, except for the one with lowest pid
REM =======================================================================================
:KILLDUPES
ECHO.&& ECHO KILLING DUPLICATE FIREFOXS
REM Check that more than 1 process is running
::tasklist.exe /V /NH /FI "IMAGENAME eq %~1" /FI "USERNAME eq SYSTEM" 2>nul | FIND /C "K Running" | FIND "1" >nul 2>&1
tasklist.exe /V /NH /FI "IMAGENAME eq %~1" 2>nul | FIND /C "K Running" | FIND "1" >nul 2>&1
IF NOT ERRORLEVEL 1 EXIT /B

REM Create Filename in current dir (of this script)
SET _CMDS=%TEMP%killdupes.log

REM Create the file in the same dir as this script named for date and sorted by PID
tasklist.exe /V /NH /FI "IMAGENAME eq %~1" 2>nul | SORT /+29 > "%_CMDS%"

REM Check that the file was created or exit
IF NOT EXIST "%_CMDS%" ( CALL :SPEAK "FILE CREATION FAILED" && EXIT /B )

REM kill all the processes found except for 1, do not kill the process with the lowest pid number
FOR /F "usebackq tokens=2 skip=2" %%p IN (`type "%_CMDS%"`) DO taskkill.exe /F /PID %%p /T >nul 2>&1

REM erase the file
ERASE /Q "%_CMDS%" >nul 2>&1

REM check that the file was erased
IF EXIST "%_CMDS%" ( CALL :SPEAK "ERASE FILE FAILED" && EXIT /B )

EXIT /B

REM =======================================================================================
REM =   SPEAK - Speak text
REM =======================================================================================
:SPEAK
ECHO %~1
EXIT /B
firefox-5min

Windows Batch File

 

 

Comments