Ubuntu – Upstart Log Not Rotating

node.jsUbuntuubuntu-12.04upstart

I have a custom upstart script that manages a Node API service. That script pipes output to /var/log/upstart/api-access.log. What I keep seeing is that the old log gets archived to api-access.log.1.gz, but a new api-access.log file isn't created.

Any idea why this might be happening or what I can do to make it right?

The script portion of my upstart file looks like this:

script
  chdir /opt/www/api
  exec sudo -u www-data NODE_ENV=production npm start >> /var/log/upstart/api-access.log
end script

Best Answer

When /var/log/upstart/api-access.log is archived the original filesystem link to it is removed, however the inode remains as it's being held by the exec-ed process. The process continues to write to this inode even while there is no reference to it and will continue to do so until the process is restarted.

Unless there a particular reason you are redirecting that output to that file you should avoid doing so. Upstart writes all output to a file in /var/log/upstart named after the upstart script anyway. It also re-opens the file on each write so the file gets recreated if it has been removed by the log rotation.

Edit by JennyD: If you do want to write to a file, set your log rotation script to copy and truncate instead of moving the file. That way, the original file handle will stay available, but the contents will be moved to an archive file.

Related Topic