def send_work(e, cmd, outToFile=None, outSuffix=None): """ e: a boto Instance object (an Amazon EC2 instance) cmd: a string After the call, e.PendingJoblist will be a list of Popen instances. e.PendingJoblist.cmd will be a list of commands (cmd). """ command_str = init_ssh_str + " root@%s %s" % (e.dns_name, cmd) if outToFile is None: job = Popen(command_str, shell=True, stdout=PIPE, stderr=STDOUT) if outToFile is not None: if outSuffix is None: stdout = file(str(outToFile) + "stdout.txt", "w") stderr = file(str(outToFile) + "stderr.txt", "w") if outSuffix is not None: stdout = file(str(outToFile) + "stdout_" + str(e).rpartition(":")[-1] + "_" + str(outSuffix) + ".txt", "w") stderr = file(str(outToFile) + "stderr_" + str(e).rpartition(":")[-1] + "_" + str(outSuffix) + ".txt", "w") job = Popen(command_str, shell=True, stdout=stdout, stderr=stderr) job.cmd = cmd e.PendingJoblist.append(job)
def _run_command(self, cmd, **args): """run an internal declared command as new subprocess""" if isinstance(cmd, list): cmdline = ' '.join(cmd) else: cmd = COMMANDS[self.package_format][cmd] if callable(cmd): try: return cmd() except IOError as err: raise LGPException(err) cmdline = Template(cmd) setup_file = self._normpath(self.config.setup_file) cmdline = cmdline.substitute(setup=setup_file, **args) self.logger.debug('run subprocess command: %s' % cmdline) if args: self.logger.debug('command substitutions: %s' % args) process = Popen(cmdline.split(), stdout=PIPE) pipe = process.communicate()[0].strip() if process.returncode > 0: process.cmd = cmdline.split() raise LGPCommandException( "lgp aborted by the '%s' command child process" % cmdline, process) if not isinstance(pipe, string_types): pipe = pipe.decode('utf-8') # py3k return pipe
def runcmd(cmd,**kw): """run a command in an child process. accepts the same parameters as the `subprocess.Popen()` constructor. An additional keyword argument `continuation` will be called with the popen object as its argument when the process terminates. The default continuation simply logs the command and return code. TODO: give an example. (Use the source, Luke.) """ cont = kw.pop("continuation",_finish) logging.debug("starting %r",cmd) p = Popen(cmd,**kw) p.cmd = cmd p.finish = cont return p
def start(request): global proc global file_list if request.method == 'POST': file_num = int(request.POST.get('id')) cmd = command.objects.get(media=file_list[file_num][1], active=True) options = command_options.objects.filter(command=cmd, active=True) popen_list = [] popen_list.append(str(cmd.command)) for option in options: popen_list.append(str(option.option)) if option.value: popen_list.append(str(option.value)) popen_list.append(str(file_list[file_num][0])) if proc and proc.poll() == None: proc.kill() proc.cmd = None proc = Popen(popen_list, stdin=PIPE, shell=False) proc.cmd = cmd response = redirect('/server/') return response return HttpResponse('nok')