crontab 不要运行php命令
Crontab do not run the php command
我正在使用带有 pheanstalk php 库的 Symfony 3 框架。我 运行 服务器上的应用程序与 Linux Debian Jesse。工作创建工作正常,如果 运行 来自终端的工作人员,它会像它应该的那样工作。但是当我将命令添加到 crontab 时,我发现该命令不起作用。 /var/main/user 没有任何登录帮助我进行调试。如果有任何帮助,我将非常高兴。
这是我的worker命令(只有execute函数):
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln("\n<info>Beanstalk worker service started</info>");
$tubes = $this->getContainer()->get('app.job_manager.power_plant')->getTubes();
if ($input->getOption('one-check')) {
// run once
foreach ($tubes as $tubeName) {
if ($tubeName != "default") {
$this->getContainer()->get('app.queue_manager')->fetchQueue($tubeName);
}
}
$output->writeln("\n<info>Beanstalk worker completed check and stoped</info>");
} else {
// run forever
set_time_limit(0);
max_execution_time(0);
while (1) {
foreach ($tubes as $tubeName) {
if ($tubeName != "default") {
$this->getContainer()->get('app.queue_manager')->fetchQueue($tubeName);
}
}
}
$output->writeln("\n<info>Beanstalk worker stoped</info>");
}
}
这是我的 app.queue_manager 从队列和 运行 命令中获取作业的函数:
public function fetchQueue($tubeName)
{
if ($this->pheanstalk->getConnection()->isServiceListening()) {
while (true === is_object($job = $this->pheanstalk->watch($tubeName)->ignore('default')->reserve(self::WATCH_TIMEOUT))) {
$data = json_decode($job->getData(), true);
$this->worker($data);
$this->pheanstalk->delete($job);
}
}
}
然后工人运行命令
public function worker($data)
{
$application = new Application($this->kernel);
$application->setAutoExit(false);
$parameters = [];
$parameters['command'] = $data['command'];
foreach ($data['meta'] as $key => $param) {
$parameters[$key] = $param;
}
$input = new ArrayInput($parameters);
$output = new NullOutput();
return $application->run($input, $output);
}
这是我不工作的 crontab:
@reboot /usr/bin/php /var/www/mose-base/bin/console beanstalk:worker:start
我创建了另一个运行正常的 cron 选项卡。它每 15 分钟工作一次,不同之处在于没有无限循环 (while(1)),因此它只通过管道一次,然后完成。但这不是我想要的。我想要无限循环,就像我用第一个 crontab 创建的那样:
*/15 * * * * /usr/bin/php /var/www/mose-base/bin/console beanstalk:worker:start --one-check
如果它在控制台中有效,则它不适用于您的文件用户权限。检查一下。
您可以通过电子邮件报告此作业的错误
*/15 * * * * /usr/bin/php /var/www/mose-base/bin/console beanstalk:worker:start --one-check 2>&1 | mail -s "mysql_dump" example@mail.example
我用了rc.local。它解决了问题。 Tnx FreudianSlip
我正在使用带有 pheanstalk php 库的 Symfony 3 框架。我 运行 服务器上的应用程序与 Linux Debian Jesse。工作创建工作正常,如果 运行 来自终端的工作人员,它会像它应该的那样工作。但是当我将命令添加到 crontab 时,我发现该命令不起作用。 /var/main/user 没有任何登录帮助我进行调试。如果有任何帮助,我将非常高兴。
这是我的worker命令(只有execute函数):
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln("\n<info>Beanstalk worker service started</info>");
$tubes = $this->getContainer()->get('app.job_manager.power_plant')->getTubes();
if ($input->getOption('one-check')) {
// run once
foreach ($tubes as $tubeName) {
if ($tubeName != "default") {
$this->getContainer()->get('app.queue_manager')->fetchQueue($tubeName);
}
}
$output->writeln("\n<info>Beanstalk worker completed check and stoped</info>");
} else {
// run forever
set_time_limit(0);
max_execution_time(0);
while (1) {
foreach ($tubes as $tubeName) {
if ($tubeName != "default") {
$this->getContainer()->get('app.queue_manager')->fetchQueue($tubeName);
}
}
}
$output->writeln("\n<info>Beanstalk worker stoped</info>");
}
}
这是我的 app.queue_manager 从队列和 运行 命令中获取作业的函数:
public function fetchQueue($tubeName)
{
if ($this->pheanstalk->getConnection()->isServiceListening()) {
while (true === is_object($job = $this->pheanstalk->watch($tubeName)->ignore('default')->reserve(self::WATCH_TIMEOUT))) {
$data = json_decode($job->getData(), true);
$this->worker($data);
$this->pheanstalk->delete($job);
}
}
}
然后工人运行命令
public function worker($data)
{
$application = new Application($this->kernel);
$application->setAutoExit(false);
$parameters = [];
$parameters['command'] = $data['command'];
foreach ($data['meta'] as $key => $param) {
$parameters[$key] = $param;
}
$input = new ArrayInput($parameters);
$output = new NullOutput();
return $application->run($input, $output);
}
这是我不工作的 crontab:
@reboot /usr/bin/php /var/www/mose-base/bin/console beanstalk:worker:start
我创建了另一个运行正常的 cron 选项卡。它每 15 分钟工作一次,不同之处在于没有无限循环 (while(1)),因此它只通过管道一次,然后完成。但这不是我想要的。我想要无限循环,就像我用第一个 crontab 创建的那样:
*/15 * * * * /usr/bin/php /var/www/mose-base/bin/console beanstalk:worker:start --one-check
如果它在控制台中有效,则它不适用于您的文件用户权限。检查一下。
您可以通过电子邮件报告此作业的错误
*/15 * * * * /usr/bin/php /var/www/mose-base/bin/console beanstalk:worker:start --one-check 2>&1 | mail -s "mysql_dump" example@mail.example
我用了rc.local。它解决了问题。 Tnx FreudianSlip