Reputation: 21
I have unicorn config file in rails application:
config/unicorn.rb
app_name = "my_app"
root = "/home/user/my_sites/#{app_name}/current"
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.err.log"
stdout_path "#{root}/log/unicorn.out.log"
listen "/tmp/unicorn.#{app_name}.sock"
worker_processes 2
timeout 30
before_exec do |_|
ENV["BUNDLE_GEMFILE"] = File.join(root, 'Gemfile')
end
How can auto rotate these log files?
For rails app and rails log I do: Rails.logger = Logger.new(Rails.root.join("log",Rails.env + ".log"), 3, 20*1024*1024)
What is the best solution for unicorn?
Upvotes: 2
Views: 7076
Reputation: 21
Under /etc/logrotate.d
create a file name called unicorn
(or the name of your app) and add below lines
/var/log/unicorn/*log {
daily
rotate 10
missingok
notifempty
compress
sharedscripts
postrotate
/bin/kill -USR1 $(cat /var/log/unicorn/unicorn.pid 2>/dev/null) 2>/dev/null || :
endscript
}
According to Unicorn's documentation:
USR1 - reopen all logs owned by the master and all workers See Unicorn::Util.reopen_logs for what is considered a log.
So /bin/kill -USR1
will send this signal to the unicorn
's process using its pid
from /var/log/unicorn/unicorn.pid
(you may need to update according to yours).
postrotate
is executed at end of logrotate
's rotation.
Upvotes: 2
Reputation: 27901
Use logrotate
See https://stackoverflow.com/a/4883967/159721 for details
Upvotes: 0