Skip to content

Commit

Permalink
WIP detach
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed Aug 26, 2015
1 parent 711ef62 commit b782526
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/awesome_spawn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,29 @@ def run!(command, options = {})
command_result
end

# Execute `command` in a detached manner
# by default the :err, and :out are redirected to `/dev/null`
# by default :pggroup is true (to make a daemon process)
# by default :pggroup_new is true (needed for windows)
# @example With normal output
# AwesomeSpawn.run_detached('echo "Hi" > /tmp/out')
#
# @example With error output as well
#
#
# if a user defines :out or :err, it is assumed they will define both
def run_detached(command, options = {})
env, command_line, options = parse_command_options(command, options)
# maybe add support later for this
raise ArgumentError, "options cannot contain :in_data" if options.include?(:in_data)

options[[:out, :err]] = ["/dev/null", "w"] unless (options.keys.flatten & [:out, :err]).any?
options[:pgroup] = true unless options.key?(:pggroup)
options[:pggroup_new] = true unless options.key?(:pggroup_new)

detach(env, command_line, options)
end

# (see CommandLineBuilder#build)
def build_command_line(command, params = nil)
CommandLineBuilder.new.build(command, params)
Expand All @@ -120,4 +143,10 @@ def parse_command_options(command, options)

[env, build_command_line(command, params), options]
end

def detach(env, command, options)
pid = Kernel.spawn(env, command, options)
Process.detach(pid)
pid
end
end

0 comments on commit b782526

Please sign in to comment.