Vamos ver como programar tarefas agendadas no CPanel em um hospedagem compartilhado para executar comandos personalizados de Symfony2.
No nosso exemplo, teremos a seguinte estrutura de arquivos do nosso projeto Symfony em nosso servidor compartilhado:
/home/{username}
|
|- /app
|- /src
| |
| |- /AppBundle
| |
| |- /Command
| |
| |-TestCommand.php
|
|- /vendor
|- /public_html
| |
| |- app.php
|
|- test_command.log
/home/{username}
|
|- /app
|- /src
| |
| |- /AppBundle
| |
| |- /Command
| |
| |-TestCommand.php
|
|- /vendor
|- /public_html
| |
| |- app.php
|
|- test_command.log
/home/{username} | |- /app |- /src | | | |- /AppBundle | | | |- /Command | | | |-TestCommand.php | |- /vendor |- /public_html | | | |- app.php | |- test_command.log
/src/AppBundle/Command/TestCommand.php
<?php
namespace AppBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class TestCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->setName('my:test:run');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Welcome World!');
}
}
<?php
namespace AppBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class TestCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->setName('my:test:run');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Welcome World!');
}
}
<?php namespace AppBundle\Command; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class TestCommand extends ContainerAwareCommand { protected function configure() { $this->setName('my:test:run'); } protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('Welcome World!'); } }
Agora fazemos rodar nosso comando cada hora e salvar o resultado no arquivo test_command.log. Para fazer isso basta ir em nosso CPanel:
Home > Advançado > Cron Jobs
Minute: 0
Hour: *
Day: *
Month: *
Weekday: *
Command: /usr/bin/php -q /home/{username}/app/console my:test:run >/home/{username}/test_command.log
Minute: 0
Hour: *
Day: *
Month: *
Weekday: *
Command: /usr/bin/php -q /home/{username}/app/console my:test:run >/home/{username}/test_command.log
Minute: 0 Hour: * Day: * Month: * Weekday: * Command: /usr/bin/php -q /home/{username}/app/console my:test:run >/home/{username}/test_command.log
Substitua {username} pelo seu nome de usuário na sua conta de hospedagem compartilhada.