I use nohup for running a command on a remote server in background, and use nohup.out file to check the progress. Can I set the nohup to re-write nohup.out as it only contains one line (the last stdout)?

  • Why not simply rm the file before starting new process ? – Sergiy Kolodyazhnyy Jan 28 '17 at 22:23
  • @Serg subtle solution. I just thought that there might be an option. – Googlebot Jan 28 '17 at 22:29

There's no option for what you ask in either external nohup and or shell-specific implementation. Couple of alternative ideas:

  • Truncate the file with redirection. Something like: nohup firefox 2>&1 > nohup.out &. Same thing are usually, but just done manually.

  • rm file before starting new command. Something like: rm nohup.out; nohup firefox 2>&1 &

  • It doesn't work. I need to have the file updated regularly. By removing it regularly, though it is somehow updated, the file is not always there to check the last stdout. – Googlebot Jan 28 '17 at 23:49

From comments, it seems as if the user wants to be able to check the output file and then empty it before checking again.

To do this, start the program with a >> redirection to the output file:

$ somecommand >>logfile &

Then read logfile in whatever manner you want. When you want to empty the file, simply do

$ >logfile

This would truncate the file but leave it in place. When the program next writes to the output file, it will write to the start of the file.

We use >> to open the file in "append mode". This means that any new writes into the file will always happen at the (current) end of the file.

Had we used > and then truncated the file, new writes in the file would have happened at the point in the file where they would originally had been placed. This means that the region between the start of the file and the new contents would have been filled with nul-bytes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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