-   Programming (
-   -   Flush server.log (

Joint PK 12-28-2009 03:14 PM

Flush server.log
Hello LQ

I have an program running, (edited.x86 = game server) and the output is logged into a server.log with 2> in the command line.

I want to backup the server.log every day This isn't a problem but, i want to make the server.log empty without restarting the server. I tried several codes


cat server.log > backup.log
echo -n > server.log

these commands do make a backup.log with the output and it does empty the server.log BUT the log doesn't save any new outputs. it just stops, but the size of the file increases constantly.

How can i fix this? or can i fix this?

Joint PK

rweaver 12-28-2009 03:27 PM

Depending on the particulars of the server you're using and the method it's using for logging you may be able to kill -HUP the process to get it reopen its file handles. Although if it wasn't originally designed for unix/linux it's likely that will disconnect players.

Joint PK 12-28-2009 03:56 PM

So if im right, your saying that i have to kill the proces, wich means that the server will go offline and restart the server?

rweaver 12-28-2009 04:35 PM

Depending on how the process handles particular signals a HUP may kill and restart it or it may just close and reopen file handles or any variety of other things. Either research it for the individual application or try it while connected and see what happens and if new file handles get opened correctly a after log rotation.

GooseYArd 12-28-2009 04:40 PM


Originally Posted by Joint PK (Post 3807191)
So if im right, your saying that i have to kill the proces, wich means that the server will go offline and restart the server?

try this:

mv server.log server.log.1

tail -f server.log.1

the server should still be writing to server.log.1

killall -HUP edited.x86

now look and see if a new server.log got created. If it did, you're golden. You just mv/kill -HUP any time you want to rotate your log.

If it didn't you'll have to try something else.

Most long lived processes interpret sighup as a request to close and reopen the logfile handle, and reread the configuration, if there is one. The idea is that you can have the process perform some housekeeping, without, say, killing connections that are open to it.

Joint PK 12-28-2009 05:08 PM

I have several servers running with the same name, the only difference is the port. Wouldn't this kill my server which makes it offline?

GooseYArd 12-28-2009 10:41 PM

nope. Kill doesn't kill processes, it sends signals to processes. Some signals normally cause a process to die. In fact, the _default_ signal that kill sends (if you don't give it one) is SIGTERM (15), which almost always stops the process.

You can send other useful signals to processes using kill also. Signals like USR1 and USR2 are reserved so that you can add a custom signal handler to respond to them. Occasionally people will use USR1 as the log rotation signal, and you'd say 'kill -USR1 12345'.

HUP or "hangup" is leftover from the days of serial comms, and wound up getting co-opted for log rotation (among other things)

Joint PK 12-29-2009 10:46 AM


Originally Posted by GooseYArd (Post 3807218)
try this:

mv server.log server.log.1

tail -f server.log.1

Till there works! but when i do


the server should still be writing to server.log.1

killall -HUP edited.x86
The server stopts, shows offline. Is there any alternative?



tail -f server.log.1 > server.log

rweaver 12-29-2009 11:22 AM

Not particularly, it's just a poorly written daemon. You'll have to stop and restart it after you rotate the logs. Perhaps schedule it for Sunday at 4am or something similar.

GooseYArd 12-29-2009 05:06 PM

I would also try sending it a sigusr1 and sigusr2, just in case they happened to use that as the log reopen signal.

The only other alternative I can think of is to copy and truncate, I can't remember if you mentioned trying that.

Youd say:

cp server.log
cat /dev/null > server.log

that way, the server continues writing to the same inode, but the file there gets truncated prior to the next write.

Joint PK 12-30-2009 12:00 PM


cp server.log /home/backup/$file/log/$DATE.log
cat /dev/null > server.log

the file still has his old size, but doesn't have anything in it

All times are GMT -5. The time now is 09:30 PM.