Score:0

Unlock a logfile locked by another process linux

ng flag

I have written a script to empty/nullify a logfile once a day on my ubuntu server.

LogFile Path: /var/log/turn.log

I have tried below commands for this purpose:

Commands : sudo cp /dev/null /var/log/turn.log sudo truncate -s 0 /var/log/turn.log

The size of the file does not reduce by using any of the either commands. I suspect that the file is locked by the process currently writing logs into it.

$ sudo fuser /var/log/turn.log
/var/log/turn.log :    964 
$ ps 964
PID    TTY    STAT    TIME    COMMAND

964    ?      Ssl    0:54     /usr/bin/turnserver -c  /etc/turnserver.conf -o -v
$

How can I unlock this file so that my script can nullify the logfile everyday at a given time.

bac0n avatar
cn flag
think you mean to do `sudo sh -c '> /var/log/turn.log'`
Soren A avatar
mx flag
Why don't you use the standard logrotate utility for this ? This is already installed and configured (in /etc/logrotate.conf and /etc/logrotate.d). See `man logrotate`. Also be aware that if a process has the logfile open, data wont be deleted from the disk until the process closes the file.
bac0n avatar
cn flag
`killall -HUP turnserver` should re-read log file.
Score:2
cn flag

How can I unlock this file so that my script can nullify the logfile everyday at a given time.

Stop turnserver before you do

sudo cp /dev/null /var/log/turn.log 
sudo truncate -s 0 /var/log

and start it after this is done. If supported only a restart after emptying the file is also possible. I would have used > /var/log/turn.log as root or from crontab as root; but this also keeps the file as is until released. Truncate is not needed.

Besides altering turnsever to not lock the file all the time (using a service I would lock the file when needed and somewhere in the loop close/reopen the log) the more generic method would be to add the log to logrotate and have logrotate deal with this: compress the file and remove compressed files when older than x days. Those older logs are never touched by turnserver.

Cyrus avatar
cn flag
`sudo > /var/log/turn.log` is wrong syntax.
Score:0
kr flag

Try this command line, it should exchange your old log file with a new empty one. The old log file will have the *.old file extension.

sudo cp -a --attributes-only /var/log/turn.log /var/log/turn.log.new &&\
sudo ln -f /var/log/turn.log /var/log/turn.log.old &&\
sudo ln -f /var/log/turn.log.new /var/log/turn.log &&\
sudo unlink /var/log/turn.log.new && sync ||\
echo "Error: Something went wrong."
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.