SA-MP Server Log Rotation
#1

Anyone that's managed a SA-MP server will know that the server log can get huge. While this doesn't impact the server performance, it makes reading it or transferring it over FTP or similar a nightmare. In addition, the log only has a timestamp, not a datestamp which can make it hard to find the exact timing of the event.

Edit: As of 0.2X, the log now contains a full time and date stamp

Therefore, I have written a batch script that will rename the server log to the current date. You can then set it to run at the end of each day. After the log has been renamed, it is placed in the log folder.



Windows Version

Copy and paste the code below into a .bat file (e.g logrotate.bat) and save it into the server directory.

This will stop and start the server. If you don't want that to happen remove the taskkill and start lines but be aware that the server must have stopped for the log rotation to work properly!

Code:
@echo off

set ServerDirectory="C:\sampserver"
set ServerName="samp-server.exe"

for /f "tokens=1-3 delims=/- " %%a in ('date /t') do set XDate=%%a-%%b-%%c

if not exist %ServerDirectory%\log mkdir %ServerDirectory%\log

echo Daily log rotator for SA-MP Servers by KingJ
echo Will rename the current log file to today's date (%DATE%)
echo and place it in the log folder.
echo.
echo Ensure the server is not running.
echo.
echo This makes the server logs easier to manage
echo.
echo Stopping server...
taskkill /f /im "%ServerName%"
echo Renaming log file to server_log_%XDate%.txt
rename %ServerDirectory%\server_log.txt server_log_%XDate%.txt
echo Moving log to log folder...
move %ServerDirectory%\server_log_%XDate%.txt %ServerDirectory%\log
echo Starting Server...
start %ServerDirectory%\%ServerName%
echo.
echo Log rotation complete
Remember to set %ServerDirectory% to the directory of your server.

Now, to automate this log rotation, we need to use the Windows task scheduler. Click Start > Control Panel > Scheduled Tasks. Then double click on "Add Scheduled Task". In the window that appears, click next then click browse and point it to the location of your .bat file. Select to perform this task Daily, click next and then set the time to 23:59. This is right at the end of the day. Click next and you may be prompted to enter your user name and password (if there is no password for you user account leave it blank). Finally, click finish and it's all done. Every day at 23:59, the script will run and rotate your log file, keeping the size down and making it easier to manage.

Linux Version

Save this as logrotate.sh

Contributed by Westie - Allows for the main log to be blanked while the server is running

Code:
#!/bin/sh

logfile=server_log.txt
timestamp=$(date --utc +%s)
thedate=date
logdir=./logs/

if [ ! -d $logdir ]
then
    mkdir $logdir
fi

cat $logfile > $logdir/server.$timestamp.txt
echo "[logs] Refreshed at UNIX TS: $thedate" > $logfile
Contributed by KaiserSouse - requires the server to be offline



Code:
FILE=server_log.txt
NOW=$(ls -l --time-style="+%b %e, %Y %k%M" $FILE | awk '{ print $6"-"$7"-"$8 }' | sed -e 's/,//g')
cp $FILE ./logs/"$NOW.$FILE"
rm -f ./server_log.txt
And add entry into Crontab to automate it.


Enjoy!
Reply
#2

Great!! Now i don't have alot of server_log_old.txt in my sa-mp folder! thanks!
Reply
#3

oops lol.

I added what I use to the bottom of the other thread. left out the part where it restarts the server tho...
Reply
#4

Quote:
Originally Posted by kaisersouse
oops lol.

I added what I use to the bottom of the other thread. left out the part where it restarts the server tho...
A server restart is not required. If the log files does not exist, it's automatically created. I'll add your code for linux to the main post.
Reply
#5

haha i always copied the server log every week to don't let it get to large
i can really use this
thank you for making this batch file
Reply
#6

NotE:

if you remove server_log.txt while the server is running...it will not automatcially recreate it.

You must stop the server
run the log rotation scrpt
restart the server
Reply
#7

My mistake, this does not work while the server is running. You have to stop the server before running this.

You can append to the end of the batch file (windows)

Code:
start %ServerDirectory%\samp-server.exe
To start the server after the log has copied. You must however kill the server yourself.
Reply
#8

maybe use something like 'serverlog managing complete, press any key to close this window'

becouse you use some echo's but nobody will be able to read them
Reply
#9

Quote:
Originally Posted by °Fallout°
maybe use something like 'serverlog managing complete, press any key to close this window'

becouse you use some echo's but nobody will be able to read them
The echos are there mainly as comments - if this is automated you don't want the command prompt hanging around for a while.

However, if you want to have the window close only when you press a key, add the following line to the end of the script.

Code:
pause
Reply
#10

yes i know that i can use pause but i thought that the echo's were for when a person runs the .bat
errm.. nvm xD
Reply
#11

Quote:
Originally Posted by Westie
With this piece of Linux magic, the server doesn't need a restart. I'll create a Windows version for this soon.
Unfortunately, it means that the existing logfile continues to grow. However, it does prevent downtime and you can delete the actual server_log.txt when needs be.

Also, unless i've misunderstood your script it copies the entire log, not just the events of the last 24hr.
Reply
#12

Quote:
Originally Posted by Westie
The 'cat' function reads the server_log.txt file, and pipes it to a new file (hense the >). Also, the timestamp is unique - it is the UNIX timestamp, bla bla (server.[timestamp].txt) The echo is to clear the old file, because what > basically means is; 'If not exist, create and write, if exist, overwrite'.
Yeah, I missed the bit where it overwrote the existing one.

In theory, it's possible to do the same thing on windows (I have the script ready to go to do the same thing) but as always the fact that the server opens exclusive access to the file hampers the ability to blank it afterwards (try echo "log clear" > server_log.txt and you get the "file is already open by another process" error).

It might be for the best if 0.2.5 includes a built in log rotator. Not difficult to add, and would please a lot of people.
Reply
#13

Is there any automated way to kill the samp-server.exe on Windows?
Reply
#14

taskkill
Reply
#15

Quote:
Originally Posted by Europe
Is there any automated way to kill the samp-server.exe on Windows?
Code:
taskkill /f /im "samp-server.exe"
This will kill *all* running version of samp-server.exe, if you want to just stop one server you can rename it to samp-server-1.exe etc.
Reply
#16

Quote:
Originally Posted by KingJ
Quote:
Originally Posted by Europe
Is there any automated way to kill the samp-server.exe on Windows?
Code:
taskkill /f /im "samp-server.exe"
This will kill *all* running version of samp-server.exe, if you want to just stop one server you can rename it to samp-server-1.exe etc.
Edit:
Nevermind and thanks I got it working!

1. It ends the SA-MP server process
2. It copies the server log to a log directory
3. It renames the server log
4. It deletes the old server log
5. It starts the SA-MP server

I'll add a sheduled task for it - very useful this
Reply
#17

I have updated the windows script on the first post with kill and restart code for the server.
Reply
#18

Quote:
Originally Posted by KingJ
I have updated the windows script on the first post with kill and restart code for the server.
you can also kill the server with the Rcon command; EXIT
Reply
#19

Quote:
Originally Posted by [NT
SpeedDevil ]
Quote:
Originally Posted by KingJ
I have updated the windows script on the first post with kill and restart code for the server.
you can also kill the server with the Rcon command; EXIT
And how you want to add that in a Windows Batch/Command file? This is about automated log rotation not manual.
Reply
#20

thanks KJ that could come in handy however a linuxx would be cool
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)