FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home » Windows » Run Firefox every 5 Min from Windows Batch File

by comment

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

Tags

Comments Welcome

Information is freedom. Freedom is non-negotiable. So please feel free to modify, copy, republish, sell, or use anything on this site in any way at any time ;)

My Online Tools

Popular Articles
Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two.
-- Richard M. Stallman


It's very simple - you read the protocol and write the code. -Bill Joy

>

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

+Askapache | htaccess.io | htaccess.guru

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service | @Htaccess

↑ TOPMain