I have a long running process on a remote computer which I started like this:
$ nohup ./process > output &
My problem is that the output file is getting bigger and bigger fast. And of course, the only use this file has to me is monitoring the progress of the process:
$ tail -f ./output
So I would like to truncate the file from time to time to conserve the space (I'm afraid I might run out of space). So I tried this:
$ truncate -s 0 output
But it seems since the file is open and being written to, this command has no effect. As a test, once I removed the file but then there wasn't a new one created. So I had lost my progress report and I had to restart the process.
Is there any way I can truncate the file while it is being written to?
While a full size solution may involve
logrotate
, it might work with a simpler solution according to the following demo example.I started a process, #1, that writes the current date and time to the file
output
.Then I started a process, #2, that copies
output
, and uses tail to truncate the redirected copy back tooutput
. This keeps writing from process #1 alive. It might work in your case too.Example of output from process #2,
I also tested how to make a demo example that does not hangup, if the window/connection is closed,
and
which can be monitored with
LANG=C tail -f ./output
from another window/connection,