mirror of
https://github.com/plashchynski/crono.git
synced 2026-06-12 17:54:25 +02:00
Daemonization
This commit is contained in:
@@ -19,12 +19,29 @@ module Crono
|
|||||||
def run
|
def run
|
||||||
parse_options(ARGV)
|
parse_options(ARGV)
|
||||||
init_logger
|
init_logger
|
||||||
|
daemonize if config.daemonize
|
||||||
|
write_pid
|
||||||
load_rails
|
load_rails
|
||||||
print_banner
|
print_banner
|
||||||
start_working_loop
|
start_working_loop
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
def daemonize
|
||||||
|
::Process.daemon(true, true)
|
||||||
|
|
||||||
|
[$stdout, $stderr].each do |io|
|
||||||
|
File.open(config.logfile, 'ab') { |f| io.reopen(f) }
|
||||||
|
io.sync = true
|
||||||
|
end
|
||||||
|
$stdin.reopen("/dev/null")
|
||||||
|
end
|
||||||
|
|
||||||
|
def write_pid
|
||||||
|
pidfile = File.expand_path(config.pidfile)
|
||||||
|
File.write(pidfile, ::Process.pid)
|
||||||
|
end
|
||||||
|
|
||||||
def init_logger
|
def init_logger
|
||||||
logfile = config.daemonize ? config.logfile : STDOUT
|
logfile = config.daemonize ? config.logfile : STDOUT
|
||||||
self.logger = Logger.new(logfile)
|
self.logger = Logger.new(logfile)
|
||||||
@@ -67,6 +84,10 @@ module Crono
|
|||||||
config.logfile = logfile
|
config.logfile = logfile
|
||||||
end
|
end
|
||||||
|
|
||||||
|
opts.on("-P", "--pidfile PATH", "Path to pidfile (Default: #{config.pidfile})") do |pidfile|
|
||||||
|
config.pidfile = pidfile
|
||||||
|
end
|
||||||
|
|
||||||
opts.on("-d", "--[no-]daemonize", "Daemonize process (Default: #{config.daemonize})") do |daemonize|
|
opts.on("-d", "--[no-]daemonize", "Daemonize process (Default: #{config.daemonize})") do |daemonize|
|
||||||
config.daemonize = daemonize
|
config.daemonize = daemonize
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user