Question

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

Was it helpful?

Solution

Two things I would suggest for this:

  1. 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 )

  2. Rename your log files with the date, otherwise you will only retain one iteration of the log. windows-batch-script-format-date-and-time

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top