I have a script to stop and start IIS services where in this script, it will define the log and the directory. The created log is growing file, since it has the same name directory (code below):
#ECHO About to stop the StackProcess
cd %windir%\system32\inetsrv
appcmd stop apppool /apppool.name:StackProcess
echo %date% %time% - Service Stopped Successfully > C:\Logs\StackProcess_IISrestart.log
#ECHO StackProcess Stopped
TIMEOUT /t 05 /nobreak
#ECHO StackProcess
cd %windir%\system32\inetsrv
appcmd start apppool /apppool.name:StackProcess
echo %date% %time% - Service Restarted Successfully > C:\Logs\StackProcess_IISrestart.log
#ECHO StackProcess Restarted
My next step was to create a log file which will named according to datestamp like "StackProcess_IISrestart-YYYYMMDD" and this log file will growing also.
echo off
set CUR_YYYY=%date:~10,4%
set CUR_MM=%date:~4,2%
set CUR_DD=%date:~7,2%
set SUBFILENAME=StackProcess%CUR_YYYY%%CUR_MM%%CUR_DD%
#ECHO About to stop the StackProcess
cd %windir%\system32\inetsrv
appcmd stop apppool /apppool.name:StackProcess
echo %date% %time% - Service Stopped Successfully > C:\Logs\%SUBFILENAME%.log
#ECHO StackProcess Stopped
TIMEOUT /t 05 /nobreak
#ECHO StackProcess
cd %windir%\system32\inetsrv
appcmd start apppool /apppool.name:StackProcess
echo %date% %time% - Service Restarted Successfully > C:\Logs\%SUBFILENAME%.log
#ECHO StackProcess Restarted
But this code were only works where the file will overwrite the existing one, which means the the log file will not growing for the day. Is there anything I miss to include code when I want it grows instead of this? Help me, thanks.
If I understand correctly, it appears that you do not understand how the redirection operator works. > starts from new, >> appends. Simply changing to the appropriate operator will fix your issue.
#Echo Off
Set "log="
For /F "Tokens=1-3Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null'
)Do If Not Defined log Set "log=StackProcess%%A%%B%%C.log"
Echo Stopping the StackProcess
"%__AppDir__%InetSrv\AppCmd.exe" Stop AppPool /apppool.name:StackProcess
Echo %DATE% %TIME% - Service Stopped Successfully>"%log%"
Echo StackProcess Stopped
Timeout 5 /NoBreak>Nul
Echo Starting the StackProcess
"%__AppDir__%InetSrv\AppCmd.exe" Start AppPool /apppool.name:StackProcess
Echo %DATE% %TIME% - Service Restarted Successfully>>"%log%"
Echo StackProcess Restarted
Timeout 2 /NoBreak>Nul
Please note, whilst I may have made some improvements in the example above, that you are logging 'successful' messages without actually verifying that the start and stop processes were successful. I'll leave it to you to decide if you wish to expand your code to cater for such verification.
Related
I used the code provided in the best answer in this thread: Need auto-restart script in batch for minecraft server
However, I'm not sure when the choice function is supposed to run.
Furthermore, I'd rather not have a choice option. I'd like the server just to give a 60 second heads up that it's going to restart and then execute the restart.
Any help would be appreciated!
Here's the code from the previous answer:
#echo off
title minecraft-server-1.8.3
color 0A
prompt [server]:
cls
:start
echo loading server...
java -Xms3G -Xmx3G -jar minecraft_server.1.8.3.jar nogui
cls
:choice
set /P a=do you want to restart[Y/N]?
if /I "%a%" EQU "Y" goto :restart
if /I "%a%" EQU "N" goto :stop
goto :choice
:restart
cls
echo server will restart
TIMEOUT /T 5
cls
goto :start
:stop
cls
echo closing server
TIMEOUT /T 5
exit
Welcome to stack overflow.
As #Mofi mentioned in a comment, you can use TIMEOUT to create wait statement
The script you might want would look something like this:
:start
echo loading server...
java -Xms3G -Xmx3G -jar minecraft_server.1.8.3.jar nogui
cls
REM I recommend NOT using TIMEOUT /T for the main wait, this way you can skip it and initiate a restart immediately
TIMEOUT 10720
REM 3 hours minus 60 seconds to allow for 60 second restart notification
cls
echo server will restart
TIMEOUT /T 60
cls
goto :start
(Since you seem to be somewhat new to batch, REM is used to comment out lines)
To answer your question surrounding why the :choice section is a thing:
It is initiated after the :start section, allowing you to either (Y) restart, or (N) stop the server
I've got a batch file that starts a .jarfile but after it finishes loading the .jar, it doesn't execute the other commands that run after.
Here's my code:
#echo off
title DWAD
color 0A
cls
:start
echo loading server...
java -Xms3G -Xmx20G -jar DWAD.jar nogui
echo "/say server will restart in 60 seconds"
TIMEOUT /T 30
echo /say server will restart in 30 seconds
TIMEOUT /T 10
echo /say server will restart in 10
TIMEOUT /T 1
echo /say 9
TIMEOUT /T 1
echo /say 8
TIMEOUT /T 1
echo /say 7
TIMEOUT /T 1
echo /say 6
TIMEOUT /T 1
echo /say 5
TIMEOUT /T 1
echo /say 4
TIMEOUT /T 1
echo /say 3
TIMEOUT /T 1
echo /say 2
TIMEOUT /T 1
echo /say 1
TIMEOUT /T 1
echo /say server restarting
How do I instruct the code to run the next command "echo" after the .jar has finished loading?
Thank you!
I've tried using a call command before java -Xms3G -Xmx20G -jar DWAD.jar nogui without luck
I've tried using a /wait command after java -Xms3G -Xmx20G -jar DWAD.jar nogui without luck too
What I expect is for the batch file to run the next line of code after loading the .jar but the result is that it doesn't. The batch file just stops right after it loads the .jar
OP would like to script a Minecraft Server
I have found the plugin I believe I used to do this in the past, it is located here:
https://www.minecraftforum.net/forums/mapping-and-modding-java-edition/minecraft-tools/1263927-win-mac-linux-minecraft-console-client
This should allow him to pass text and commands to the user sin the server while performing functions on the OS too, allowing him to automate the server's shut down as he's looking to do.
Double checked and again looks like the one I used 8 years ago when I had a server for a bit, and as the plugin author says:
• Single command usage : MinecraftClient.exe username password server
"/mycommand"
This will automatically send "/mycommand" to the server, and then the
client will automatically leave the server and close.
Useful for use in batch command sending scripts.
This would change your script to be something like this:
#(
SETLOCAL
ECHO OFF
SET "_UN=ServerUserName"
SET "_PW=ServerPassword"
SET "_IP=ServerIP"
title MinecraftClient
color 0A
cls
)
CALL :Main
(
ENDLOCAL
Exit /B 0
)
:Main
ECHO.
ECHO. Begining Script:
REM Letting the Users Know about the Count Down
FOR /L %%L IN (60,-1,1) DO (
MinecraftClient.exe %_UN% %_PW% %_IP% "/say WARNING: Server will Restart in %%L Seconds!"
Timeout 1
)
ECHO.
ECHO. Sending Final Notice and Shutting down the Server!
MinecraftClient.exe %_UN% %_PW% %_IP% "/say !!! WARNING: Server GOING DOWN FOR RESTART NOW !!!"
REM Not sure what the Restart command is offhand.
MinecraftClient.exe %_UN% %_PW% %_IP% "/Restart Command"
GOTO :EOF
So, I have a bat file I've been building to do a simple profile backup/restore. I'm running Windows 7 64bit.
I get through 99% of the script until it gets to this point and then dies. To be clear, in the environment I'm working in, I HAVE to turn off UAC for certain things. Someone much higher up than me made that call.
However, this needs to do this but it dies after the "[ COMPLETE ]" is echoed. It doesn't close the window, it just goes back to the C prompt.
The only other thing it's going (trying) to do after that is log the user off after a 15 second timer.
Any help would be much appreciated.
echo [ Turning off UAC... ]
C:\Windows\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
ping -n 2 -w 1000 127.0.0.1 > nul
echo [ COMPLETE ]
ping -n 2 -w 1000 127.0.0.1 > nul
echo.
ECHO ==========================================================
ECHO = Restore Complete =
ECHO = Computer will logoff in 15 seconds to apply changes. =
ECHO ==========================================================
TIMEOUT 16
shutdown /l
#pause
:EOF
I made a test batch file that does what you're trying:
#echo off
echo before
cmd /k echo I'm doing it!
echo after
And here was the result:
C:\files\j>test
before
I'm doing it!
So I changed it to call:
#echo off
echo before
call echo I'm doing it!
echo after
And got this:
C:\files\j>test
before
I'm doing it!
after
So clearly, /k isn't for you. But CALL will put it in a new shell... So my recommendation is to just change that line to be :
%windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
No cmd, no \k, no anything - just run the REG.exe.
If you have to, use the CALL since you're adding a registry key and it should stay there.
As part of a solution I've just implemented, I'd like to automate a housekeeping schedule for log files maintenance.
I know what I've put together is a bit crude but it does the job anyway. Just want someone to take a quick look and fine tune the script if possible?
Requirement - Ideally, I would want the script to:
1. Stop two services.
2. Check if they are indeed stopped by verifying their error level status.
3. If they are in hung state, identify the process and kill the same.
4. Upon completion of the stop / kill, move two specific log files to archive folder
5. Start the services back again.
6. Nice to have - make sure they are both in running state (error level = 4).
My script:
echo off
:sub_StopServices
net stop sasl_svn
echo ======================================================
echo %date% %time% SASL service stopped
net stop SVNServe
echo ======================================================
echo %date% %time% SVN service stopped
::set level=%errorlevel%
sc query sasl_svn | find "STATE" | FIND "RUNNING" >NUL
sc query svnserve | find "STATE" | FIND "RUNNING" >NUL
if %errorlevel%==1 ( if %errorlevel%==1 (GOTO sub_moveLogs)
else (GOTO sub_StopServices))
if %errorlevel%==3 (taskkill /f /im svnserve.exe && taskkill /f /im saslauthd.exe)
else (GOTO sub_moveLogs))
:sub_MoveLogs
MOVE E:\cygwin\var\log\sasl_svn.log E:\cygwin\var\log\Archive\sasl_svn.log
MOVE E:\cygwin\var\log\SVNServe.log E:\cygwin\var\log\Archive\SVNServe.log
echo ======================================================
echo %date% %time% Log archive is successful
GOTO sub_StartServices
:sub_StartServices
net start sasl_svn
echo ======================================================
echo %date% %time% SASL service started
net start SVNServe
echo ======================================================
echo %date% %time% SVN service started
exit
Thanks
Karthik Durairajan
Two things I would suggest for this:
Don't outright kill the service exe, it may be completing a long running operation
SC STOP VCCDataService>nul
:waitforstop
SET ServiceToKill=[service name]
tasklist /nh /fi "imagename eq %ServiceToKill%" | find /i "%ServiceToKill%" >nul && (
echo %ServiceToKill% is running
TIMEOUT /T 15
GOTO waitforstop
) || (
echo %ServiceToKill% is not running
)
Rename your log files with the date, otherwise you will only retain one iteration of the log. windows-batch-script-format-date-and-time
I'm currently running a StarMade Server and am working on having the server auto restart.
Right now we have 2 batch files, 1 that runs the server, and another to restart the server.
The batch file running the server is:
title Main Starmade Server
java -Xms4096m -Xmx6144m -XX:PermSize=512m -XX:MaxPermSize=1024m -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+UseNUMA -XX:+CMSParallelRemarkEnabled -XX:MaxGCPauseMillis=50 -XX:+UseAdaptiveGCBoundary -XX:-UseGCOverheadLimit -XX:+UseBiasedLocking -XX:SurvivorRatio=8 -XX:TargetSurvivorRatio=90 -XX:UseSSE=3 -XX:+UseLargePages -XX:+UseFastAccessorMethods -XX:+UseStringCache -XX:+UseCompressedOops -XX:+AggressiveOpts -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintTenuringDistribution -Xloggc:memory.log -Xincgc -Xshare:off -jar StarMade.jar -server
EXIT
And the batch file restarting the server is:
#echo off
title Server Automated Restarter
:start
echo Starting Starmade Server
set time=60
set timer
cd C:\StarMade\StarMade
start StartServer.bat /REALTIME
timeout 120
echo Server Started
timeout 5
:loop
cls
IF %time% GTR 0 (
set /a time=%time% -1
set /a min=%time%/60 +1
echo Server Restarter Active
echo Next Restart In %time% Seconds; Less Than %min% Minutes Remaining
cd C:\Windows\System32
ping (my ip goes here, but I've removed it for this post) -n 2 > NUL
goto loop
)
echo Killing Java Task
timeout 5
taskkill /f /im java.exe
timeout 30
echo Restarting Starmade Server
cls
goto start
Obviously, this technically crashes the server is it is force stopped. The game has a "/force_save" command and a "/shutdown (timeInSec)" command. I would like to be able to use the automated restarter bat to send those commands to the other batch file which is running the server. While the server is running, no other commands and be called within the batch file running the server (unless manually typed).