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)?
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 &
logfile in whatever manner you want. When you want to empty the file, simply do
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.
>> 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.
nohup without any redirection the file
nohup.out is written in
That's why the truncation of the file after inspection by the command
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.
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.
rmfile before starting new command. Something like:
rm nohup.out; nohup firefox 2>&1 &