Sha256: df71c0c5e7258208ab8116183e33903484e3ebdebffc7373c1805942347a8034

Contents?: true

Size: 772 Bytes

Versions: 14

Compression:

Stored size: 772 Bytes

Contents

# run command/block as a background daemon
module Wunderbar
  def submit(cmd=nil)
    fork do
      # detach from tty
      Process.setsid
      fork and exit

      # clear working directory and mask
      Dir.chdir '/'
      File.umask 0000

      # close open files
      STDIN.reopen '/dev/null'
      STDOUT.reopen '/dev/null', 'a'
      STDERR.reopen STDOUT

      # clear environment of cgi cruft
      require 'cgi'
      ENV.delete_if {|key,value| key =~ /^HTTP_/}
      CGI::QueryExtension.public_instance_methods.each do |method|
        ENV.delete method.to_s.upcase
      end

      # setup environment
      ENV['USER'] ||= $USER
      ENV['HOME'] ||= $HOME

      # run cmd and/or block
      system cmd if cmd
      yield if block_given?
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
wunderbar-0.12.3 lib/wunderbar/job-control.rb
wunderbar-0.12.1 lib/wunderbar/job-control.rb
wunderbar-0.12.0 lib/wunderbar/job-control.rb
wunderbar-0.11.1 lib/wunderbar/job-control.rb
wunderbar-0.11.0 lib/wunderbar/job-control.rb
wunderbar-0.10.9 lib/wunderbar/job-control.rb
wunderbar-0.10.8 lib/wunderbar/job-control.rb
wunderbar-0.10.7 lib/wunderbar/job-control.rb
wunderbar-0.10.6 lib/wunderbar/job-control.rb
wunderbar-0.10.5 lib/wunderbar/job-control.rb
wunderbar-0.10.4 lib/wunderbar/job-control.rb
wunderbar-0.10.3 lib/wunderbar/job-control.rb
wunderbar-0.10.2 lib/wunderbar/job-control.rb
wunderbar-0.10.1 lib/wunderbar/job-control.rb