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?
Related
I have created the below code to execute multiple commands depending on the user input but the input is case-sensitive.
I have tried the /I switch with the if statement but that does not work either.
Below is the code:
#Echo off
SET /P uname=Launch Outlook in safe mode:
IF "%uname%"==Yes GOTO Error
IF "%uname%"==No GOTO start
:Error
taskkill /IM Outlook.exe /f
cd c:\Program Files (x86)\Microsoft Office\root\Office16
start Outlook.exe /safe
#echo The Script is running please wait && timeout /t 20
taskkill /IM Outlook.exe /f
cd %Homepath%
cd Appdata\Local\Temp
echo.>myfile.txt && #echo Thanks for using the script,You may close this window.and continue. > myfile.txt
start outlook.exe
timeout /t 8
start notepad myfile.txt
:End
:start
echo "No valid options"
echo "This window would close in 10 seconds" && timeout /t 10
:End
I would use the choice command instead
echo Y] Yes
echo N] No
choice /c yn /n
if %errorlevel%==1 goto yes
if %errorlevel%==2 goto yes
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
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.
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
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