Linux – Rotating a nohup.out logfile

linuxlogrotatenohup

There are a few questions on how to rotate the output file generated by nohup. Most of the answers say to use logrotate with the copytruncate option.

However this does not actually work — when the rotation occurs, it creates the rotated logfiles ok but the original file is not actually truncated and further log output continues to be appended.

This also occurs if I don't use nohup and just run myscript.py >>myscript.log (so basically copytruncate seems completely useless).

What's the "right" way to do this? (If custom script is required, either Bash or Python is preferred.)

Best Answer

The correct way to do this is to code myscript.py so that it writes to a log file rather than stdout and reacts to signals by closing and reopening it's log file.