在ruby中使用守护进程在不同的目录中添加日志
我正在使用守护进程包装我的脚本,并在其中指定了日志位置: 脚本如下所示:在ruby中使用守护进程在不同的目录中添加日志,ruby,daemon,logrotate,daemons,Ruby,Daemon,Logrotate,Daemons,我正在使用守护进程包装我的脚本,并在其中指定了日志位置: 脚本如下所示: #!/usr/local/bin/ruby require 'rubygems' require 'daemons' Daemons.run_proc( 'script_test', # name of daemon :log_output => true, :output_logfilename => "script-test.log", :logfilename => "sc
#!/usr/local/bin/ruby
require 'rubygems'
require 'daemons'
Daemons.run_proc(
'script_test', # name of daemon
:log_output => true,
:output_logfilename => "script-test.log",
:logfilename => "script-test.log"
) do
exec 'ruby /opt/script-test/script-test.rb'
end
问题是我的日志存储在脚本所在的同一目录中。我必须将日志添加到不同的目录,如/var/log/script test,然后每周轮换这些日志。
为我提供一个解决方案,以便我可以将脚本的日志存储在/var/log目录中。请确保使用的是绝对路径而不是相对路径 例如:
:output_logfilename => "/var/log/script-test.log",
:logfilename => "/var/log/script-test.log"
为了对日志进行日志轮换,(假设使用Linux),请在logrotate
config中添加以下内容,以便每周轮换一次:
/var/log/script-test.log {
weekly
missingok
compress
notifempty
copytruncate
}
我的配置如下:
Daemons.run_proc(
'script-test', # name of daemon
:log_output => true,
:dir_mode => :normal,
:dir => "/var/log",
:output_logfilename => "script-test.log",
:logfilename => "script-test.log"
) do
exec 'ruby /opt/script-test/script-test.rb'
结束也许你可以尝试一些宝石,就像我改变的那样,但没有运气。这不会在/var/log目录中创建任何日志文件。感谢您共享logrotate脚本。作为临时测试,请先尝试使用
sudo touch/var/log/script test.log
创建文件,然后使用sudo chmod 777/var/log/script test.log
。