diff --git a/lib/awesome_spawn.rb b/lib/awesome_spawn.rb index 88f0c45..5f0d08c 100644 --- a/lib/awesome_spawn.rb +++ b/lib/awesome_spawn.rb @@ -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) @@ -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