长时间运行的脚本的 symfony 执行
Symfony execution of long running script
我正在使用Symfony2,我想运行一个用C++编写的长脚本(例如60分钟)。
现在我通过shell_exec()
来做到这一点:
$pid = shell_exec('nohup my/program/written/in/c++.out some arguments > /dev/null 2>/dev/null & echo $!');
如果我不断刷新页面,脚本运行良好,但是如果我使用 AFK,脚本将随着 PHP 服务器的进程 (/usr/bin/php-cgi) 终止。
有没有办法将程序与PHP服务器进程隔离C++?使用 nohup 时,进程的 ppid = 1,因此应该将其隔离,但事实并非如此。
你可以
看看Symfony进程组件: http://symfony.com/doc/current/components/process.html
$process = new Process('nohup my/program/written/in/c++.out some arguments');
$process->run();
您将能够运行您的进程。
你可以创建 symfony2 控制台命令 "myapp:my-command-name" 来运行你的 c++ 命令
class MyStandaloneCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->setName('myapp:my-command-name')
->setDescription('Will run standalone c++')
->addArgument('arg1', InputArgument::REQUIRED, 'Some arg');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$arg1 = $input->getArgument('arg1');
$result = shell_exec('nohup my/program/written/in/c++.out '.$arg1.' 2>&1');
$output->writeln('My cool command is started');
return true;
}
}
然后使用 JMSJobBundle
http://jmsyst.com/bundles/JMSJobQueueBundle/master/installation
您可以在其中创建控制台命令的队列,如下所示:
class HomeController ... {
// inject service here
private $cronJobHelper;
// inject EM here
private $em;
public function indexAction(){
$job = $this->cronJobHelper->createConsoleJob('myapp:my-command-name', $event->getId(), 10);
$this->em->persist($job);
$this->em->persist($job);
$this->em->flush();
}
use JMSJobQueueBundleEntityJob;
class CronJobHelper{
public function createConsoleJob($consoleFunction, $params, $delayToRunInSeconds, $priority = Job::PRIORITY_DEFAULT, $queue = Job::DEFAULT_QUEUE){
if(!is_array($params)){
$params = [$params];
}
$job = new Job($consoleFunction, $params, 1, $queue, $priority);
$date = $job->getExecuteAfter();
$date = new DateTime('now');
$date->setTimezone(new DateTimeZone('UTC')); //just in case
$date->add(new DateInterval('PT'.$delayToRunInSeconds.'S'));
$job->setExecuteAfter($date);
return $job;
}
}
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- C++,系统无法执行指定的程序
- 使用C++中的模板和运算符重载执行矩阵运算
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 执行函数时导致崩溃的变量
- 无论条件是否为true,if总是在c++中执行
- 当函数模板参数是具有默认参数的类模板时,函数模板参数的推导如何执行
- 在C++中对T*类型执行std::move的意外行为
- 使用QProcess执行命令,并将结果存储在QStringList中
- 如何在没有信号的情况下从C++执行QML插槽
- 如何确认我的constexpr表达式实际上已经在编译时执行
- C++17中的并行执行策略
- QML按钮点击功能执行顺序
- 程序在执行程序的其余部分之前退出
- 为什么catch中的代码没有被执行
- C++从其他 constexpr 创建 lambda 不能按顺序执行 Constexpr
- 将执行、作业和WinAPI相乘
- 对字符数组中的元素执行逐位操作
- 为什么g++在未执行的代码处标记强制转换错误
- 长时间运行的脚本的 symfony 执行