I have a script that writes to a logfile like this:
$ nohup myscript.sh > myscript.out 2>&1 &
when the log file gets very large, I need to truncate it like this:
> myscript.out
I see the size go to 0 briefly but immediately jumps back to full size again.
$ ls -ald myscript.out
-rw-rw-r-- 1 vmware vmware 14285855 Apr 11 04:33 myscript.out
$ > myscript.out
$ ls -ald myscript.out
-rw-rw-r-- 1 vmware vmware 0 Apr 11 04:33 myscript.out
$ ls -ald myscript.out
-rw-rw-r-- 1 vmware vmware 14298778 Apr 11 04:33 myscript.out
How can I truncate it so the size goes to zero and starts growing again from zero?
I have tried many other alternatives but nothing works. Same thing, size goes to 0, then back to full size.
true > myscript.out
: > myscript.out
echo -n > myscript.out
cp /dev/null myscript.out
truncate -s 0 myscript.out
dd if=/dev/null of=myscript.out
du -h myscript.out
. It might be usual to close a logfile on each append to flush it, or perhaps rotate it and remove older versions later.stderr
orstdout
for logging is a bad idea - the log file and how big it is becomes locked to your running process. There are good reasons why logging such assyslog
is built into operating systems. You have to kill your running process, then clean up the log file.