Sha256: 97d1a1c3f666cf703b5265031cdb6cc065f5945574b95295861a054149320cb4

Contents?: true

Size: 929 Bytes

Versions: 7

Compression:

Stored size: 929 Bytes

Contents

require 'wunderbar'
require 'rbconfig'

# run command/block as a background daemon
module Wunderbar
  def self.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'
      if RbConfig::CONFIG['host_os'] =~ /darwin|mac os/
        STDOUT.reopen '/dev/null'
      else
        STDOUT.reopen '/dev/null', 'a'
      end
      STDERR.reopen STDOUT

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

      # run cmd and/or block
      system({'USER' => $USER, 'HOME' => $HOME}, cmd) if cmd
      yield if block_given?
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
wunderbar-1.6.0 lib/wunderbar/job-control.rb
wunderbar-1.5.1 lib/wunderbar/job-control.rb
wunderbar-1.5.0 lib/wunderbar/job-control.rb
wunderbar-1.4.5 lib/wunderbar/job-control.rb
wunderbar-1.4.2 lib/wunderbar/job-control.rb
wunderbar-1.4.1 lib/wunderbar/job-control.rb
wunderbar-1.4.0 lib/wunderbar/job-control.rb