0

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.)

Miral
  • 169
  • 1
  • 7
  • 1
    Copytruncate should work. However, the right way to do it is described in lain answer because copytruncate may be responsible of small data loss. – Xavier Lucas Oct 11 '14 at 21:03

1 Answers1

1

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.

user9517
  • 114,104
  • 20
  • 206
  • 289
  • Ok, granted. But I want to know how to do it without doing that. (The script is a third party one and is large and difficult to modify.) – Miral Oct 11 '14 at 21:14
  • @Miral If copytruncate really doesn't work, use a [named pipe](http://serverfault.com/questions/189477/rotate-logs-of-a-dumb-non-interactive-application/189880#189880) – user9517 Oct 11 '14 at 21:19
  • Use your script in pipeline: `3rd_party_script.py | your_script 3rd_party_script.log` – AnFi Oct 11 '14 at 22:32