Batch File to Archive Logs


The AlphaLB logs are "locked" while AlphaLB is running and so cannot be archived, renamed or deleted unless the service is stopped.


The batch file below is a simple script to archive the two AlphaLB logs to an archive folder when (and only when) the log size has exceeded a configured maximum size.


Recycling the AlphaLB will not kick off any users or cause a problem with session variables.


Choose ONE of the options below.

Option 1 - NET STOP/START method


  1. Create a dos batch file named AlphaLB_ArchiveLogs.bat using the text below.
  2. Add this line at the top of the batch file: NET STOP "Alpha Load Balancer (managed by AlwaysUpService)"
  3. Add this line at the bottom of the batch file but above the EXIT command: NET STOP "Alpha Load Balancer (managed by AlwaysUpService)"
    1. *Note - your service name might be different, please check Windows Services for the Alpha Load Balancer.
  4. Place this batch file in the AlphaLB folder, typically c:\alphalb.
  5. Use your own method to schedule when to fire this batch file.

Option 2 - AlwaysUp method


Most of us are using AlwaysUp and it has a convenient method to call a batch file to run prior to starting the AlphaLB.


  1. Create a dos batch file named AlphaLB_ArchiveLogs.bat using the text below.
  2. Place this batch file in the AlphaLB folder, typically c:\alphalb.
  3. Go to AlwaysUp and right-click on the Alpha Load Balancer application and select Edit.
  4. Go to the Startup tab, click "Run the following program/batch file" and select the AlphaLB_ArchiveLogs.bat file.
  5. Save the changes.
  6. If you have not already set the AlphaLB to restart on some schedule, go to the Monitor tab (not the Restart tab) tab and select "Every" and select a time period such as "Every Sunday at 1:00AM".


Batch File Notes

  • If desired, you can change the following settings in the batch file:
    • maxBytes is set to 10485760 which is 10MB. You can change this to any integer value, for example 5MB is 5242880 bytes.
    • arcfolder is set to _archive. You can set this to any valid folder including outside of the Alpha LB folder such as arcfolder=c:\otherfolder\logs.
    • file is set to the two AlphaLB log files. If you happen to have additional log files in this folder, you can list them delimited by an embedded space.
  • Caution - do not place a "pause" statement in this batch file or your application will not restart.


Batch File Content


setlocal enabledelayedexpansion

REM 10485760 IS 10MB

set maxBytes=10485760

set arcfolder=_archive


md %arcfolder%

set file=AlphaLB_Access.log AlphaLB_Info.log

set YYYY=%DATE:~10,4%

set MM=%DATE:~4,2%

set DD=%DATE:~7,2%

set HH=%TIME: =0%

set HH=%HH:~0,2%

set MI=%TIME:~3,2%

set SS=%TIME:~6,2%

set FF=%TIME:~9,2%


FOR %%f IN (%file%) DO (

IF %%~zf GTR %maxBytes% (

 move %%f %arcfolder%/%%~nf_%YYYY%%MM%%DD%_%HH%%MI%%SS%.log

  )

)

EXIT