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

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.

| improve this answer | |

Just using nohup without any redirection the file nohup.out is written in append mode.

That's why the truncation of the file after inspection by the command

> nohup.out

sets the file to length zero and subsequent output of the program is written to nohup.out at the start of the file.

That is exactly what the above answer to the question told - and it works. This could be useful for lengthy output where after the inspection of the current state of the run the contents of the file is not interesting anymore.

As the question indicated the output of the program is just one line, so a rm nohup.out before the next call gives the same result as > nohup.out with the sole difference that the file doesn't exist after its removal while there remains a zero-length file otherwise.

| improve this answer | |

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 &

| improve this answer | |
  • 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

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.