integrosys
asked on
Clear contents of a file
Let's say that I have a Java program running, with its standard output directed to a log file:
java MyJavaProgram > stdout.log
This program will keep on running. To ensure that the log file does not grow very big, I am trying to write a script to remove the contents of the file. I tried two ways:
echo "" > stdout.log
cp /dev/null stdout.log
But in both cases, they are writting a lot of null characters into the log file. The file does not shrink at all. The contents are merely replaced with null characters.
How can I achive what I want? Thanks.
java MyJavaProgram > stdout.log
This program will keep on running. To ensure that the log file does not grow very big, I am trying to write a script to remove the contents of the file. I tried two ways:
echo "" > stdout.log
cp /dev/null stdout.log
But in both cases, they are writting a lot of null characters into the log file. The file does not shrink at all. The contents are merely replaced with null characters.
How can I achive what I want? Thanks.
You can only reliably truncate a file when it's not being written to.
Do you have any control over the java program, ie: can you restart it?
Easiest way to truncate a file is
>stdout.log
Do you have any control over the java program, ie: can you restart it?
Easiest way to truncate a file is
>stdout.log
ASKER
Thanks guys. Actually I found that these two lines:
echo "" > stdout.log
cp /dev/null stdout.log
will work if the Java program is started this way instead:
java MyJavaProgram >> stdout.log
echo "" > stdout.log
cp /dev/null stdout.log
will work if the Java program is started this way instead:
java MyJavaProgram >> stdout.log
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi blu, what is a sparse file? You said that it does not allocate storage blocks. But when I use the ls command, I notice that the file size never decreases.
If you use
du -sh file
You'll see the real size of the file.
du -sh file
You'll see the real size of the file.
Or use "ls -ls"
rm -f stdout.log
touch stdout.log
This is probably not the most ideal scenario as you "may" need to reset user / group permissions.