Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

i have the program running using this command

command 2> sample.txt

now that file is growing continuously and command will exit in 5-6 days and i beleive that file size won't go in GB

I tried this

echo "" > sample.txt but thats not making any differnce to it and filesize is growing.

i was thinking of setting up cron job after 1 hour to empty its contents

How can i empty the contents of file

share|improve this question
    
What does cp /dev/null sample.txt do for you? –  Bernhard Dec 20 '12 at 13:03
    
See also unix.stackexchange.com/q/46549/977 on Unix.SE, I wrote a perl program there that you can use in the future to keep only the last X bytes of output. –  derobert Dec 26 '12 at 22:23

4 Answers 4

Since the file is held open by the application, I don't think you can just empty it traditionally. Googling a bit I found this:

Assuming that the author of the code writing the never-ending file knows even a little bit about unix:

  1. For this process there is a configuration file, which names the output logfile.
  2. The process "reconfigures" itself when it receives a SIGHUP signal, by re-reading the config file, and if the name of the logfile changed, then close the old one, open the new one.

This is pretty much UNIX standard for a never-ending log writer program.

Other than that, I can't find any obvious solution, since you used > and not >>. Next time you might want to redirect through truncate or something of the sort to avoid this problem of an infinitely-open logfile.

share|improve this answer
    
i could not understood , what should i do next time, i mean the complete command –  user1858027 Dec 20 '12 at 14:09
    
The never ending file is being written by shell redirection, so unfortunately HUP isn't going to be useful. Truncate isn't going to be helpful, as you can't pipe (redirect) through it. But >> would help. –  derobert Dec 26 '12 at 22:17

All you need is:

cat /dev/null > sample.txt

or

>sample.txt

share|improve this answer
    
You meant cat, not cp. cp would complain cp: missing destination file operand after `/dev/null' Try `cp --help' for more information. Except of course OP said he already tried echo "" > sample.txt, which is basically the same thing –  derobert Dec 26 '12 at 22:12
    
@derobert Thanks, I will update my answer. –  ponsfonze Dec 26 '12 at 23:26

The following command should work:

cat /dev/null > sample.txt  
share|improve this answer

Use logrotate, it is aimed exactly for this mission

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.