aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/process/daemon.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support/core_ext/process/daemon.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/process/daemon.rb25
1 files changed, 25 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/process/daemon.rb b/activesupport/lib/active_support/core_ext/process/daemon.rb
new file mode 100644
index 0000000000..95ad5f8a5d
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/process/daemon.rb
@@ -0,0 +1,25 @@
+if RUBY_VERSION < "1.9"
+ module Process
+ def self.daemon(nochdir = nil, noclose = nil)
+ exit if fork # Parent exits, child continues.
+ Process.setsid # Become session leader.
+ exit if fork # Zap session leader. See [1].
+
+ unless nochdir
+ Dir.chdir "/" # Release old working directory.
+ end
+
+ File.umask 0000 # Ensure sensible umask. Adjust as needed.
+
+ unless noclose
+ STDIN.reopen "/dev/null" # Free file descriptors and
+ STDOUT.reopen "/dev/null", "a" # point them somewhere sensible.
+ STDERR.reopen '/dev/null', 'a'
+ end
+
+ trap("TERM") { exit }
+
+ return 0
+ end
+ end
+end