Let's see how to program tasks scheduled in CPanel shared hosting to run custom commands of Symfony2.
In our example we will have the following file structure of our Symfony project in our shared server:
/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!'); } }
Now we run each time our command and to save the result in the test_command.log file. To do this just go in our CPanel:
Home > Advanced > 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
Replace {username} by your user name on your shared hosting account.