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

Har*_*cla 6 php linux cron symfony

我在 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.');
}
}
Run Code Online (Sandbox Code Playgroud)

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

在 linux 中我做了 sudo : nano crontab -e和 cron :

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

我做错了什么,你能帮我吗?提前谢谢,对不起我的英语

Pet*_*ter 5

我可以把它写得很短。您将需要使用 PHP 来执行控制台。

* * * * * php -q /usr/bin/php /var/www/myProject/bin/console desktop:auction_end > /dev/null 
Run Code Online (Sandbox Code Playgroud)

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

* * * * * php -q /usr/bin/php /var/www/myProject/bin/console desktop:auction_end > /home/<user>/crons/auction_end.cron.txt 
Run Code Online (Sandbox Code Playgroud)

编辑

可能应该将 yourphp用作绝对路径。

* * * * * /path/to/php /path/to/bin/console symfony:command
Run Code Online (Sandbox Code Playgroud)

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

* * * * *  root /usr/bin/php /path/to/bin/console command:to:execute
Run Code Online (Sandbox Code Playgroud)

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