protected function execute(InputInterface $input, OutputInterface $output) { $procApp = $input->getArgument('procApp'); $procCall = $input->getArgument('procCall'); $procArgs = $input->getArgument('processArgs'); $cmd = 'php ' . $procApp . ' ' . $procCall . ' ' . implode(' ', $procArgs); $process = new Process($cmd); $process->start(); echo 'PID: ' . $process->getPid() . "\n"; }
/** * Starts the server * * @param array $options * @return void */ public function start(array $options) { $verbose = isset($options['verbose']) && $options['verbose']; if ($this->checkServer()) { $this->output->writeln("<error>Queue Manager is already running.</error>"); return; } if ($verbose) { $this->output->writeln("<info>Queue Manager is starting.</info>"); } $phpFinder = new PhpExecutableFinder(); $phpPath = $phpFinder->find(); if ($options['daemon']) { $command = $phpPath . ' app/console naroga:queue:start ' . ($verbose ? '-v' : '') . ' &'; $app = new Process($command); $app->setTimeout(0); $app->start(); $pid = $app->getPid(); $this->memcache->replace('queue.lock', $pid); if ($verbose) { $this->output->writeln('<info>Queue Manager started with PID = ' . ($pid + 1) . '.</info>'); } return; } $this->registerListeners($verbose); if ($verbose) { $pid = $this->memcache->get('queue.lock'); $this->output->writeln('<info>Queue Manager started with PID = ' . $pid . '.</info>'); } $this->resetServerConfig($options); $this->processQueue($options); }
/** * Starts a process in the background - waits for 1 second to check that the process did not die prematurely * (it is supposed to be a long-running task) * @param string $command * * @return int the pid of the created process * @throws \Symfony\Component\Process\Exception\RuntimeException when process could not start / terminated immediately */ public function startProcess($command) { $process = new Process($command); $process->start(); // give the OS some time to abort invalid commands sleep(1); if (!$process->isRunning()) { throw new RuntimeException("Process terminated immediately"); } // while at it, emit a message if ($this->dispatcher) { $event = new ProcessStartedEvent($process->getPid(), $command); $this->dispatcher->dispatch(EventsList::PROCESS_STARTED, $event); } return $process->getPid(); }
/** * @param InputInterface $input * @param OutputInterface $output * @return bool */ protected function execute(InputInterface $input, OutputInterface $output) { $process = new Process($input->getArgument('argument')); $process->start(); $pid = $process->getPid(); $entityManager = $this->getContainer()->get('doctrine.orm.entity_manager'); $entity_process = new \Monotype\Bundle\DirectControllBundle\Entity\Process(); $entity_process->setPid($process->getPid()); $entity_process->setScript($input->getArgument('argument')); $entity_process->setUid('manual'); $entity_process->setDatetime(new \DateTime('now')); $entityManager->persist($entity_process); $entityManager->flush(); $output->writeln('Command "' . $input->getArgument('argument') . '" result:'); $output->writeln('PID: ' . $pid . PHP_EOL); return true; }
/** * @param InputInterface $input * @param OutputInterface $output * @return bool */ protected function execute(InputInterface $input, OutputInterface $output) { $process = new Process('watch df -h'); $process->start(); $pid = $process->getPid(); $output->writeln('Command result:'); $output->writeln('PID: ' . $pid . PHP_EOL); return true; }
public function newWorker() { $queue = Input::get('queue'); $root = base_path(); $cmd = 'php artisan worker:start ' . $queue; $p = new Symfony\Component\Process\Process($cmd, $root); $p->start(); return Response::json(array('res' => 0, 'pid' => $p->getPid())); }
protected function start_proc(&$entry) { $process = new Process($entry["command"], $this->cwd, $this->env); $process->start(); $entry["pid"] = $process->getPid(); $entry["process"] = $process; if (empty($entry["pid"])) { throw new \Exception($entry["name"] . " could not be run"); } }
/** * {@inheritdoc} */ public function run(JobReportInterface $report) { try { $this->process->start(); $this->savePid($this->process->getPid(), $report); while ($this->process->isRunning()) { // waiting for process to finish } if ($this->process->isSuccessful()) { $report->setOutput(trim($this->process->getOutput())); return JobState::STATE_FINISHED; } $report->setErrorOutput(trim($this->process->getErrorOutput())); } catch (LogicException $e) { $report->setErrorOutput($e->getMessage()); } catch (RuntimeException $e) { $report->setErrorOutput($e->getMessage()); } return JobState::STATE_FAILED; }
/** * Run child process. * * @param Daemonize $daemon * * @return mixed */ public function runChildProcess(Daemonize $daemon) { $this->output->setEnableAnsi(false); $this->output->setStdOut($this->log); $this->output->setStdErr($this->log); $this->process->start(); if (!$this->process->isRunning()) { throw new RuntimeException('Unable to start server process.'); } $this->processControl->setPid($this->process->getPid()); $this->process->wait(function ($type, $buffer) { $this->output->writeln($buffer); }); }
/** * @param Process $process * @param string $pidFile * @param OutputInterface $log * * @throws \Exception on failure * * @return int * The process PID. */ public function startProcess(Process $process, $pidFile, OutputInterface $log) { $this->processes[$pidFile] = $process; try { $process->start(function ($type, $buffer) use($log) { $log->writeln($buffer); }); } catch (\Exception $e) { unset($this->processes[$pidFile]); throw $e; } $pid = $process->getPid(); if (file_put_contents($pidFile, $pid) === false) { throw new \RuntimeException('Failed to write PID file: ' . $pidFile); } $log->writeln(sprintf('Process started: %s', $process->getCommandLine())); return $pid; }
protected function execute(InputInterface $input, OutputInterface $output) { $startTime = microtime(true); $tables = $this->getContainer()->getParameter('tables'); $limit = (int) $input->getOption('limit'); $commands = []; foreach (array_keys($tables['root']) as $root) { $commands[] = "kp:scrap:page {$root}"; } foreach ($tables['child'] as $root => $tables) { foreach (array_keys($tables) as $child) { $commands[] = "kp:scrap:sub:page {$root} {$child}"; } } /* @var $processes Process[] */ $processes = []; foreach ($commands as $command) { $processes[$command] = $process = new Process("app/console {$command} --limit={$limit}"); $output->writeln("start: <info>{$command}</info>"); $process->start(); } while (true) { foreach ($processes as $command => $process) { if ($process->isRunning()) { $output->writeln("Running {$command}: <info>{$process->getPid()}</info>"); } else { $output->writeln("Finish <info>{$command}</info>:"); $output->writeln($process->getOutput()); $processes[$command] = $process->restart(); } sleep(1); } $duration = microtime(true) - $startTime; $output->writeln("<info>Time: {$duration}</info>"); } }
private function runCommand($string, $timeout = 0) { $entityManager = $this->managerRegistry->getManagerForClass('DspSoftsCronManagerBundle:CronTaskLog'); try { $process = new Process($string); $process->setTimeout($timeout); $process->start(array($this, 'callbackProcess')); $this->cronTaskLog->setPid($process->getPid()); $entityManager->persist($this->cronTaskLog); $entityManager->flush(); $exitCode = $process->wait(); $this->cronTaskLog->setDateEnd(new \DateTime()); if ($exitCode > 0) { $this->cronTaskLog->setStatus(CronTaskLog::STATUS_FAILED); } elseif ($process->getErrorOutput() != '') { $this->cronTaskLog->setStatus(CronTaskLog::STATUS_WARNING); } else { $this->cronTaskLog->setStatus(CronTaskLog::STATUS_SUCCESS); } $this->cronTaskLog->setPid(null); $entityManager->persist($this->cronTaskLog); $entityManager->flush(); return $exitCode !== 0; } catch (\Exception $e) { $this->cronTaskLog->setStatus(CronTaskLog::STATUS_FAILED); $this->cronTaskLog->setPid(null); $this->cronTaskLog->setDateEnd(new \DateTime()); $entityManager->persist($this->cronTaskLog); $entityManager->flush(); return 1; } }
/** * Lock the event * * @param \Crunz\Event $event * * @return string */ protected function lock() { file_put_contents($this->lockFile(), $this->process->getPid()); }
/** * @param $command * @param bool $isRestore */ protected function runProcessAsync($command, $isRestore = false) { $process = new Process($command); $process->start(); $pid = $process->getPid(); $activePids = Yii::$app->session->get('backupPids', []); if (!$process->isRunning()) { if ($process->isSuccessful()) { $msg = !$isRestore ? Yii::t('dbManager', 'Dump successfully created.') : Yii::t('dbManager', 'Dump successfully restored.'); Yii::$app->session->addFlash('success', $msg); } else { $msg = !$isRestore ? Yii::t('dbManager', 'Dump failed.') : Yii::t('dbManager', 'Restore failed.'); Yii::$app->session->addFlash('error', $msg . '<br>' . 'Command - ' . $command . '<br>' . $process->getOutput() . $process->getErrorOutput()); Yii::error($msg . PHP_EOL . 'Command - ' . $command . PHP_EOL . $process->getOutput() . PHP_EOL . $process->getErrorOutput()); } } else { $activePids[$pid] = $command; Yii::$app->session->set('backupPids', $activePids); Yii::$app->session->addFlash('info', Yii::t('dbManager', 'Process running with pid={pid}', ['pid' => $pid]) . '<br>' . $command); } }
public function startDisplayProcess() { if ($this->isWindows()) { $cmd = null; } else { if ($this->getEnvVar('DISPLAY')) { return true; } $this->setEnvVar('DISPLAY', ':99.0'); $cmd = '/sbin/start-stop-daemon --start --pidfile /tmp/custom_xvfb_99.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :99 -ac -screen 0 1280x1024x16'; } $output = new BufferedOutput(); $process = new Process($cmd, SeleniumSetup::$APP_ROOT_PATH, SeleniumSetup::$APP_PROCESS_ENV, null, null); $process->run(function ($type, $line) use($output) { $output->write($line); }); //var_dump($process->getPid()); return $process->getPid(); }
/** * @param $command * @return int|null */ public function executeBackgroundCommand($command) { $process = new Process($this->_getCommandString($command) . ' > /dev/null &'); $process->start(); return $process->getPid(); }
/** * Run single task. * * @param Task $task * @param int $timeout * * @return void */ public function run($task, $timeout) { $process = new Process($task->getEntity()->getCommandline()); $process->setTimeout($timeout); $process->start($this); $task->defer($process->getPid(), function () use($process) { $process->wait($this); return $process->getExitCode(); }); }
/** * Execute one job. * * @param Job $job */ protected function executeJob(Job $job) { $job->setStartDate($this->now); $command = 'php app/console campaignchain:job ' . $job->getId() . ' --env=' . $this->getContainer()->get('kernel')->getEnvironment(); $process = new Process($command); $this->logger->info('Executing Job with command: ' . $command); $process->setTimeout($this->timeout); try { $process->mustRun(); $this->logger->info('Process ID: ' . $process->getPid()); $job->setPid($process->getPid()); $this->em->flush(); } catch (ProcessFailedException $e) { $errMsg = 'Process failed: ' . $e->getMessage(); $this->logger->error($errMsg); $this->logger->info(self::LOGGER_MSG_END); $job->setPid($process->getPid()); $job->setStatus(Job::STATUS_ERROR); $job->setMessage($errMsg); $this->em->flush(); } }
/** * This function will allow your cron script to run commands in parallel and wait until they all finish * * @param null $specificTime * @param OutputInterface $output */ function runDueCommandsParallel($specificTime = null, OutputInterface $output = null) { global $argv; if (count($argv) > 1) { if ($output == null) { $output = new ConsoleOutput(); } $this->application->run(null, $output); } else { $commands = $this->getDueCommands($specificTime); if ($output == null) { $output = new ConsoleOutput(); } $processes = []; foreach ($commands as $command) { $output->writeln("Starting " . $argv[0] . ' ' . $command); $process = new Process($argv[0] . ' ' . $command); $process->start(); $processes[] = $process; } while (true) { $close = true; foreach ($processes as $process) { /** @var Process $process */ if ($process->isRunning()) { $output->writeln($process->getCommandLine() . ' still running under ' . $process->getPid()); $close = false; } } if ($close) { break; } sleep(1); } } }
/** * Run the job * * @param JobEntity $job Job * * @return int return code */ public function run(JobEntity $job) { // Mark job running $this->markJobRunning($job, getmypid()); // Last check, that we did not exceed the concurrency limit $running = $this->getRunningJobs(); $maxRunning = $this->getConcurrency(); if (count($running) > $maxRunning) { // Rollback $this->markJobWaiting($job); return -1; } // Execute $cmd = sprintf('php %s %s', $this->consolePath, $job->getCommand()); $process = new Process($cmd); try { $process->setTimeout(null); $process->run(); } catch (\Exception $e) { // @rfe log more info $msg = sprintf('Job %s (ID %d) failed with exception: %s', $job->getCommand(), $job->getId(), $e->getMessage()); $this->getLogger()->err($msg); $this->markJobFailed($job); return -1; } // log output $this->getLogger()->info(sprintf('Process output (%d): %s', $process->getPid(), $process->getOutput())); $exitCode = $process->getExitCode(); // Mark as done if (!$exitCode) { $this->markJobDone($job); } else { $this->markJobFailed($job); } return $process->getExitCode(); }