繁体   English   中英

如何作为cron作业执行Symfony命令?

[英]How to execute a Symfony command as cron job?

我在symfony中有这个简单的命令:

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class AuctionEndCommand extends Command
{
protected function configure()
{
    error_log(print_r('test',true), 3, "/tmp/error.log");

    $this->setName('desktop:auction_end')->setDescription('Execute when auction is end.')->setHelp("Identify winners for auctions");
}
protected function execute(InputInterface $input, OutputInterface $output)
{

    // outputs multiple lines to the console (adding "\n" at the end of each line)
    $output->writeln([
        'User Creator',
        '============',
        '',
    ]);

    // outputs a message followed by a "\n"
    $output->writeln('Whoa!');

    // outputs a message without adding a "\n" at the end of the line
    $output->write('You are about to ');
    $output->write('create a user.');
}
}

现在当我执行: /var/www/myProject/bin/console desktop:auction_end这个命令工作正常。 但是当我尝试在linux中作为cron执行时,这个脚本无法启动:

在我作为sudo的linux中我做过: nano crontab -e和cron:

* * * * * /usr/bin/php /var/www/myProject/bin/console desktop:auction_end > /dev/null

我做错了什么,你能帮帮我吗? Thx提前和抱歉我的英语

你执行cron的用户是什么? Symfony需要对(取决于版本)app / cache && app / log(s)||的写访问权限 var / cache && var / log(s)如果您执行命令的用户没有对您尝试写入的目录的写访问权,那么您的操作将失败。

修复此错误的一种好方法是检查错误日志,在var / log(s)app / log(s)中或检查/var/log/apache2/error.log中的apache2错误日志我猜其中一个这些日志将包含您的问题的提示。

我可以保持很短的时间。 您将需要使用PHP来执行控制台。

* * * * * php -q /usr/bin/php /var/www/myProject/bin/console desktop:auction_end > /dev/null 

将正在测试的cron作业的输出写入文件将帮助您调试错误。 现在所有的输出都在虚空中丢失了:)

* * * * * php -q /usr/bin/php /var/www/myProject/bin/console desktop:auction_end > /home/<user>/crons/auction_end.cron.txt 

编辑

可能是你的php应该被用作绝对路径。

* * * * * /path/to/php /path/to/bin/console symfony:command

或者甚至通过指定用户执行命令:

* * * * *  root /usr/bin/php /path/to/bin/console command:to:execute

还要确保root用户有权在symfony项目中执行文件。

如果你以root身份运行它,它是否有效:

* * * * * root php /var/www/myProject/bin/console desktop:auction_end > /dev/null

看看是否有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM