aboutsummaryrefslogtreecommitdiffstats
path: root/script/runner
diff options
context:
space:
mode:
Diffstat (limited to 'script/runner')
-rwxr-xr-xscript/runner41
1 files changed, 38 insertions, 3 deletions
diff --git a/script/runner b/script/runner
index e5f52ff8e..5a5254c47 100755
--- a/script/runner
+++ b/script/runner
@@ -1,5 +1,40 @@
#!/usr/bin/ruby
-#!/usr/bin/env ruby
-require File.dirname(__FILE__) + '/../config/boot'
-require 'commands/runner' \ No newline at end of file
+daemon_mode = !ARGV.empty? && ARGV[0] == "--daemon"
+
+script_dir = File.dirname(__FILE__)
+alaveteli_dir = File.join(script_dir, "..")
+
+Dir.chdir(alaveteli_dir) do
+ require File.join(alaveteli_dir, 'config', 'boot')
+ if daemon_mode
+ # Run in daemon mode.
+
+ # If the environment variable LOGFILE is present,
+ # redirect STDERR and STDOUT to that file.
+ if ENV.has_key? "LOGFILE"
+ STDERR.reopen(STDOUT.reopen(ENV["LOGFILE"], "a"))
+ STDOUT.sync=true
+ puts "Daemon starting at #{Time.new}"
+ end
+
+ # Load the runner in a subprocess
+ pid = fork do
+ require 'commands/runner'
+ exit 0
+ end
+
+ # If the environment variable PIDFILE is present,
+ # write the pid of the daemon process to that file.
+ if ENV.has_key? "PIDFILE"
+ File.open(ENV["PIDFILE"], 'w') do |fh|
+ fh.puts pid
+ end
+ end
+
+ Process.detach(pid)
+ else
+ # Not daemon mode
+ require 'commands/runner'
+ end
+end