I'm using the script utility to record terminal sessions, and using logrotate (with copytruncate
, script ignores HUP and keeps writing to the rotated file otherwise) to periodically upload chunks of what's happened so far to another server. This works great for the the actual screen contents.
Today I'm trying to also capture timing information, which script emits to STDERR. So my new script config looks like
script -faqt session.record 2> session.timing
The problem is that when I use logrotate on the timing data (session.timing) the backfilled file begins with a large number of null bytes in the output, which causes scriptreplay to hork when it reads the reconstructed file back in.
Is there a known problem (and hopefully workaround) using logrotate with the copytruncate option on STDERR redirected into a file?
You need to redirect stderr using
2>>
(append). This is an issue with files opened without theO_APPEND
flag and logrotate's copytruncate.If I can't fix it, I can work around it by adding a postrotate script like:
Which will strip out any NULL bytes (since they're illegal anywhere in script timing files) of the first rotated file.
Presumably in a rotated file where NULL is sometimes OK, you could alter that sed command to strip just NULL bytes from the beginning of line zero in the file.