Run Batch File That Stops Process at Specified Time - batch-file

I'm running a batch file to start a Minecraft server. Here is the following script:
#echo off
"%ProgramFiles%\Java\jre7\bin\java.exe" -Xms1024M -Xmx1024M -jar craftbukkit-1.4.7-R1.0.jar
pause
It just occurred to me that perhaps its possible for the batch file to automatically stop the process on a specified time of day, but not through a countdown of any sort.
In addition, I had hoped that it can write the following lines in the command prompt:
say Server is closing in 15 minutes (1:45am)
say Server is closing in 10 minutes (1:50am)
say Server is closing in 5 minutes (1:55am)
say Server is closing, goodbye. (2:00am)
stop (2:00am)
If this is not possible, any recommendation would be appreciated.

Is this what you're looking for?
#echo off
setlocal enabledelayedexpansion
cmd /c "%ProgramFiles%\Java\jre7\bin\java.exe" -Xms1024M -Xmx1024M -jar craftbukkit-1.4.7-R1.0.jar
:waitloop
for /f "tokens=1-3 delims=:." %%I in ("%time%") do (
if "%%I"==" 1" (
if "%%J"=="45" (
echo ! Server is closing in 15 minutes.
) else if "%%J"=="50" (
echo ! Server is closing in 10 minutes.
) else if "%%J"=="55" (
echo ! Server is closing in 5 minutes.
)
) else if "%%I"==" 2" (
echo ! Toodloo.
wmic process where name="java.exe" delete >NUL 2>NUL
goto :EOF
)
set /a secs=60 - %%K + 1
ping 127.0.0.1 -n !secs! >NUL
)
goto waitloop

Related

Power Management Batch File to shutdown pc when another pc shuts down/hibernates/looses connectivity

this is my first post here so please excuse me for getting anything wrong.
I have a few computers that run Octane renderer. One is the "Master" and the others are "Slaves". When the Master is unreachable the Slaves can't render so I'd like to save money and the environment by having a small batch script for when this happens.
My thinking to a solution was to have a batch script on the Slaves to ping the Master every 5 mins to check there is a connection. When the ping fails it tries again 3 times and if it fails every time the Slave shuts down.
I've got this script from another post here that I did minor edits to : Batch File to reboot pc on network connectivity loss but it doesn't work for what I need. When I run it with the ping ip set to the Master's ip it says my connection is active. When attempting to ping the Master in a command prompt I get "destination host unreachable". Any help would be greatly appreciated!
set ping_ip=1.1.1.1
set failure_count=0
set timeout_secs=300
set connection_error_count=0
set max_connection_error_count=3
:start
:: calling the ping function
call :connection_test
:: Processing the network "up" state
if "%network_state%"=="up" (
echo INFO: You have an active connection.
set connection_error_count=0
) else (
set /a connection_error_count+=1
)
:: Processing the network "down" state
if "%network_state%"=="down" (
if %connection_error_count% geq %max_connection_error_count% (
echo ERROR: You do not have an active connection.
goto poweroff
) else (
echo INFO: FAILURE: That failed [%connection_error_count%] times, NOT good. lets try again...
goto start
)
)
timeout /t %timeout_secs%
goto start
:: connection_test function
goto skip_connection_test
:connection_test
:: Getting the successful ping count
echo INFO: Checking connection, please hang tight for a second...
for /f "tokens=5 delims==, " %%p in ('ping -n 4 %ping_ip% ^| findstr /i "Received"') do set ping_count=%%p
:: Check the ping_count against the failure_count
if "%ping_count%" leq "%failure_count%" (
set network_state=down
) else (
set network_state=up
)
goto :eof
:skip_connection_test
:: Power off
:poweroff
echo INFO: Shutdown PC in 60 seconds. Press any key to abort.
shutdown -s -t 60 -f
pause > nul
shutdown -a
goto end
:end ```
Your code is quite large for something so simple. First, the address you set on ipv4 is not the correct address!
You can access my repository to read how to acquire your IPv4 code here.
After obtain your IPv4 address, add to your code:
ping %ipv4% -n 9 | findstr /i "unreach">filex.txt
set /p stop=<filex.txt
if "%stop%" neq "" (
:: put here your shutdown preferences.
)
If you want something like what you are searching for:
#echo off
:verif
set points=
echo INFO: Checking connection, please hang tight for a second...
ping %ipv4% -n 3 | findstr /i "unreach">filex.txt
set /p stop=<filex.txt
if "%stop%" neq "" set /a points=%points%+1
del /q /f filex.txt
ping %ipv4% -n 3 | findstr /i "unreach">filed.txt
set /p stopd=<filed.txt
if "%stopd%" neq "" set /a points=%points%+1
:: Add points as you want.
if "%points%" geq "2" (
set shutdown=y
)
if "%shutdown%" equ "y" (
echo ERROR: You do not have an active connection.
shutdown -s -t 60 -f
echo INFO: Shutdown PC in 60 seconds. Press any key to abort.
pause>nul
shutdown -a
goto verif
)
echo INFO: You have an active connection.
goto verif
Hope this helps,
K.

Msgbox to prompt user if job (printing) is skip in batch

As suggested in my previous question, 1 question per thread. So Im here to open another question. Basically I want to prompt user that printing will be skip today because nothing to print, then the user will press OK, then code will continue to shutdown the computer. I want to do this to alert user that today's printing job have been run.
So I try some code like below, it seems working but i dont know how to implement in my main code.
#echo off
Call :Msgbox
if "%errorlevel%"=="1" GOTO SHUTDOWN
exit /b
:Msgbox
echo wscript.quit MsgBox ("Printing skipped.. Press ok to shutdow the computer", 48, "Shutdown Computer") >"%temp%\input.vbs"
wscript //nologo "%temp%\input.vbs"
exit /b
:SHUTDOWN
echo "%SystemRoot%\System32\shutdown.exe -s -t 60"
PAUSE
This is part of my main code where i want to place above code.
https://stackoverflow.com/a/57609600/6409413
IF pdf file size greater than 9000byte > Print PDF for today > Then go to Shutdown
IF pdf file size less than 9000byte > Promp user using msgbox > user press OK > Skip Print PDF for today > Then go to Shutdown
Rem ------------------------------------------------------------------------
Rem 4. Printing files with sizes over 9000 bytes
Set "_Exe1=%ProgramFiles(x86)%\Foxit Software\Foxit Reader\Foxit Reader.exe"
For %%A In ("%_Dir1%\c\%_FullDateString%.pdf")Do If %%~zA GTR 9000 (
Echo Printing %%A&Echo=&"%_Exe1%" /t "%_Dir1%\c\%_FullDateString%.pdf")
UPDATE:
Sort of working for now using code below. Any others way to achieve this?
Rem ------------------------------------------------------------------------
Rem 4. Printing files with sizes over 9000 bytes
Set "_Exe1=%ProgramFiles(x86)%\Foxit Software\Foxit Reader\Foxit Reader.exe"
setlocal EnableDelayedExpansion
For %%A In ("%_Dir1%\c\%_FullDateString%.pdf")Do ( set size=%%~zA
if !size! GTR 9000 (
goto PRINT
) else if !size! LSS 9000 (
goto NOPRINT
:NOPRINT
msg * /time:0 /w Printing skipped.. Press "OK" to shutdown the computer"
goto SHUTDOWN
)
)
:PRINT
Echo "Printing %%A&Echo=&"%_Exe1%" /t "%_Dir1%\c\%_FullDateString%.pdf")"
Rem ------------------------------------------------------------------------
Rem 5. Shutting down computer
:SHUTDOWN
echo "%SystemRoot%\System32\shutdown.exe -s -t 60"
PAUSE
I really think that you're overcomplicating this unnecessarily. You're already using the shutdown command, which can pop up a dialog box to the end user.
Rem ------------------------------------------------------------------------
Rem 4. Printing files with sizes over 9000 bytes
Set "_Exe1=%ProgramFiles(x86)%\Foxit Software\Foxit Reader\Foxit Reader.exe"
Set "_Msg=skipped"
For %%A In ("%_Dir1%\c\%_FullDateString%.pdf")Do If %%~zA GTR 9000 (
Set "_Msg=finished"&Echo Printing %%A&Echo=
"%_Exe1%" /t "%_Dir1%\c\%_FullDateString%.pdf")
Rem ------------------------------------------------------------------------
Rem 5. Shutting down computer
ShutDown /S /T 60 /C "Printing %_Msg%, shutting down your computer." /D P:0:0
Note: This section is a direct replacement for the previous code I provided, it is not based upon whatever you've posted above, (which is littered with issues).

Second instance of batch loop does not run

I am trying to create a DOS batch script on my Windows 7 machine. I want to execute file 123456.bat, which contains the following command and parameters:
call **startSelenium.bat** 55 someSuiteName suite-someSuite.html development 1 1 10 10
That script calls into the startSelenium.bat script below:
Setlocal EnableDelayedExpansion
SET TimeStamp=
FOR /f "tokens=1-4 delims=/ " %%i in ("%date%") do set datestr=%%l%%j%%k
FOR /f "tokens=1-4 delims=.: " %%i in ("%time%") do set timestr=%%i%%j%%k%%l
SET TimeStamp=%datestr%%timestr%
set a=2
set b=0
set /a c=%a%+%b%
FOR /l %%t IN (1, 1, %c%) do (
call :SEARCHPORT %%t
echo %startPort%
Start java -jar C:\Selenium\2\selenium-server-standalone-2.47.1.jar -port %startPort% -singleWindow -userExtensions C:\selenium\2\user-extensions.js -firefoxProfileTemplate "c:\selenium\2\ffprofiles\2rc" -htmlSuite "*chrome" "https://www.google.com" "Z:\selenium\2\environment\%4\suites\%3" "u:\results\%4\result-%1-%computername%-1234-%TimeStamp%.htm"
timeout /t 10
)
GOTO :EOF
:SEARCHPORT
netstat -o -n -a | find "LISTENING" | find ":%startPort% " > NUL
if "%ERRORLEVEL%" equ "0" (
set /a startPort +=1
GOTO :SEARCHPORT
) ELSE (
set freePort=%startPort%
echo %startPort%
GOTO :EOF
When I run the script, the first instance of the java applications runs with a free Windows port that the SEARCHPORT subroutine found; however, the second instance pops up and quits immediately. I suspect the code is using the same variable from the first time it went through the FOR loop instead of getting a new unused port number.
What am I doing wrong? I copied various parts of this code from other sources. I am obviously a nube, so plain English would be helpful. :)
You have got an delayed expansion issue.
You already enabled delayed expansion, but you don't use it. You have to replace %startPort% with !startPort! inside your for /l loop.

What is wrong with goto command in for loop?

I have a list of computers that I want to make some things according to tcp connection status.
I'm trying to check tcp connection and if errorlog is "1" so write line to log and skip to next computer.
The problem is that when a computer has no tcp connection the goto skip_action command takes the script to the end and exit and the other computers in the list left unprocessed.
I have also tried to use goto :eof and it terminates the script unexpected.
ipst.txt file:
1 10.1.1.10
3 10.1.3.10
8 10.1.3.10
This is the batch file code:
#echo off
setlocal enabledelayedexpansion
set computerslist=ipst.txt
for /f "usebackq tokens=1,2" %%A in ("%Computerslist%") do (
cls
set Station_Num=%%A
set Comp_IP=%%B
ping !Comp_IP! -n 1 | findstr "TTL"
if !errorlevel!==1 (
echo Station !Station_Num! .... !Comp_IP! ..................... No Communication.>>%log%
goto skip_action
)
echo Getting Administrator Credentials:
net use \\!Comp_IP! /USER:WORKGROUP\****** ******
echo.
xcopy file.txt \\!Comp_IP!\c\temp\
echo Disconneting Session From Remote Computer :
net use \\!Comp_IP! /DELETE /YES
:skip_action
echo end of working on !Station_Num!
)
echo end of script
The problem here is that GOTO cancels the for loop.
But you can simply enclose your action in an ELSE block
for /f "usebackq tokens=1,2" %%A in ("%Computerslist%") do (
cls
set Station_Num=%%A
set Comp_IP=%%B
ping !Comp_IP! -n 1 | findstr "TTL"
if !errorlevel!==1 (
echo Station !Station_Num! .... !Comp_IP! ..................... No Communication.>>%log%
) Else (
echo Getting Administrator Credentials:
net use \\!Comp_IP! /USER:WORKGROUP\****** ******
echo.
xcopy file.txt \\!Comp_IP!\c\temp\
echo Disconneting Session From Remote Computer :
net use \\!Comp_IP! /DELETE /YES
)
echo end of working on !Station_Num!
)
General form of if - else statement
If condition (
Statements
) else (
Statements to be executed if condition is not met
)
Make your call from the loop
You can have a function ping .. and parse variable references to it .. the preceding code could also be rendered as %%a and %%b being the first and second argument respectively -- in your function call ...
Call :pingcomputers %%a %%b
This way you avoid set altogether
:pingcomputers
Rem Pinging computer %~1
Ping %~2 ¦ find "reply"
If errorlevel 1 goto failed
(Actions to be performed if ping wa successful)
Assuming that your script has a label failed and actions to.be performed if error level is not 0 ..
Not much of a script but hope it helps ...

How to start a batch file when a text file is created?

I am trying to work out how to run a batch file when a text file is created. I have tried to research but am unable to find anything that helps.
The Pseudocode of what I want is something like this:
When "Response.txt" is created run "2 Week.bat".
Thanks.
#echo off
:start_loop
if exist "c:\Response.txt" (
start cmd /c "c:\2weeks.bat"
goto :exodus
)
rem :: wait for 1 minute
w32tm /stripchart /computer:localhost /period:1 /dataonly /samples:60 >nul 2>&1
goto :start_loop
:exodus
This will work on Vista and later:
#echo off
:loop
if exist "c:\folder\Response.txt" "c:\folder\2 week.bat"
rem - reduce CPU usage by waiting for 15 seconds
timeout /t 15 /nobreak
goto :loop

Resources