We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Controller action as a cron job phalcon

I need to run a cron job to do a sql query and save the data in the database every 24 hours.

I have a controller called GetproductsController.php and the method I have to execute is indexAction()

I've tried to do it this way:

59 23 * cd /var/www/html/proyecto/getproductscontroller.php index

But I don´t know how to do it, If anyone knows how I can do it, I would appreciate it.



1.9k

Everything is explained here https://docs.phalcon.io/en/latest/reference/cli.html

And also you will need php in front of file in your cron line

edited Feb '17

yes do it with cli or use curl on your crontab.

59 23 * curl --silent https://domain.com/products

Thanks for your answers! That´s good, and how can I add another controller I mean I have this in MainTask.php:

class MainTask extends Task{

public function mainAction(){
    echo "This is the default task and the default action" . PHP_EOL;
}

public function getProductivityAction(){

  $production   = array();
  $year         = date('Y');
  $sqlSowing =" SELECT CONCAT(YEAR(date),' ') AS year
  FROM sw_sowing
  WHERE type = 'SW'
  AND status = 100
  AND id_tenant = :id_tenant
  GROUP BY YEAR(date)
  ORDER BY date";
  $resultYear = $this->db->fetchAll($sqlSowing, Phalcon\Db::FETCH_ASSOC, array(
  ':id_tenant' => $this->getIdTenant()
));

} }

but the query it doesn´t work because I need another controller call ControllerBase.php, how can I add another extends in the class?

edited Feb '17

But in curl I use /usr/bin/curl ?, I am new in this jeje!

I try this but it doesn´t work

5,10,15,20,25,30,35,40,45,50,55,59 /usr/bin/curl --silent https://localhost:8080/proyecto/reportsowing/getproductivity

yes do it with cli or use curl on your crontab.

59 23 * curl --silent https://domain.com/products



3.4k
Accepted
answer
edited Feb '17

You can either set up tasks (see my StackOverflow answer on how to do that - https://stackoverflow.com/questions/41874177/phalcon-cli-task-name/41875667#41875667) and call it via php -f /path/to/cli.php <<TaskName>> <<ActionName>> OR use wget/curl to rbasically immitate the same request a browser would make.

The cron command would be either;

# Make sure the /path/to/cli.php is executable
# chmod +x /path/to/cli.php
59 0 * * * php -f /path/to/cli.php <<TaskName>> <<ActionName>> >/dev/null 2>&1

OR

59 0 * * * wget -q https://domain.com/products >/dev/null 2>&1

You can then run either the php -f or wget manually to see if everything is fine. Let me know how well this helps you.

Thanks for your answer I am going to prove it !

You can either set up tasks (see my StackOverflow answer on how to do that - https://stackoverflow.com/questions/41874177/phalcon-cli-task-name/41875667#41875667) and call it via php -f /path/to/cli.php <<TaskName>> <<ActionName>> OR use wget/curl to rbasically immitate the same request a browser would make.

The cron command would be either;

# Make sure the /path/to/cli.php is executable
# chmod +x /path/to/cli.php
59 0 * * * php -f /path/to/cli.php <<TaskName>> <<ActionName>> >/dev/null 2>&1

OR

59 0 * * * wget -q https://domain.com/products >/dev/null 2>&1

You can then run either the php -f or wget manually to see if everything is fine. Let me know how well this helps you.

Everything is explained here https://docs.phalcon.io/en/latest/reference/cli.html

And also you will need php in front of file in your cron line

Just a suggestion to always show the relevant parts from links, the link is gone so now "nothing is explained". ;-)