How to set phalcon cli for cron job on cpnael

Hi everyone. i want to set up cronjob on cpanel with phalcon cli already configured but the cron job never triggers.. this is the line of code : /usr/local/bin/php /home/sharks/public_html/cronjob/app clip.php main

Do any errors happen? I assume you have a MainTask.php file? With a mainAction() method?

edited 10d ago

thanks for your response. i have the mainTask.php.. i've followed the phalcon console instruction in setting up console tasks but now i want to run a cronjob from cpanel that will point to something like this app/cli.php main but it's not triggered so i'm wondering what i did wrong... check this line in the cronjob set /usr/local/bin/php /home/sharks/public_html/cronjob/app/cli.php main

Do any errors happen? I assume you have a MainTask.php file? With a mainAction() method?

Really hard to tell without any code or errors. Can you run that command directly from the command line to see what happens?

Some services like the router and dispatcher don't work in the command line, so if you're using a common bootstrap file, you'll need to disable them for the command line.

edited 10d ago

Kindly check the lines below... i think where am missing it on the cpanel's cronjob is the right way to point to the file that will be executed.. i've followed the cli sturcture from phalcons documentation. i ran it on command prompt and it worked but not on the cronjob

cli.php

<?php

use Phalcon\Config\Adapter\Ini as ConfigIni;

require_once('./vendor/autoload.php');

defined('APP_PATH') || define('APP_PATH', realpath(dirname(FILE)));

$loader = new \Phalcon\Loader();

$loader->registerDirs(array( APP_PATH . '/tasks' ));

$loader->registerNamespaces(array( 'Sid\Phalcon\Cron' => DIR . '/../vendor/phalcon-cron/src/', 'Cron' => DIR . '/../vendor/cron-expression/src/Cron', 'Phalcon\Ext\Mailer' => DIR . '/../vendor/phalcon-ext/mailer/src/', 'Multiple\Frontend\Component' => DIR . '/components/', 'Multiple\Frontend\Models' => DIR . '/models/', ));

$loader->register();

$config = new ConfigIni(APP_PATH . '/config/config.ini');

$di = new \Phalcon\Di\FactoryDefault\Cli();

$di->setShared('config', function() use ($config){ return $config; });

$di->setShared( "cron", function () { $cron = new \Sid\Phalcon\Cron\Manager();

    $cron->add(
        new \Sid\Phalcon\Cron\Job\Phalcon(
            "* 8 * * *",
            [
                "task"   => "main",
                "action" => "main",
            ]
        )
    );

    $cron->add(
        new \Sid\Phalcon\Cron\Job\Phalcon(
            "* 8 * * *",
            [
                "task"   => "main",
                "action" => "expire",
            ]
        )
    );

    $cron->add(
        new \Sid\Phalcon\Cron\Job\System(
            "* 8 * * *",
            "php cli.php main"
        )
    );

    return $cron;
}

);

$arguments = array();

foreach($argv as $keysflow => $valuesflow){ if($keysflow == 1){ $arguments['task'] = $valuesflow; } elseif($keysflow == 2){ $arguments['action'] = $valuesflow; } elseif($keysflow >= 3){ $arguments['params'][] = $valuesflow; } }

define('CURRENT_TASK', (isset($argv[1]) ? $argv[1] : null)); define('CURRENT_ACTION', (isset($argv[2]) ? $argv[2] : null));

$console = new \Phalcon\Cli\Console(); $console->setDI($di); $di->setShared("console", $console);

$di->set('db', function() use ($di){ //Use the database configure in the config file $dbConfig = $di->get('config')->get('db')->toArray(); $dbOption = array( PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES \'UTF8\'", PDO::ATTR_CASE => PDO::CASE_LOWER ); //Use the database configure in the config file return new \Phalcon\Db\Adapter\Pdo\Mysql($dbConfig+$dbOption); });

$di->set('security', function(){ //$security->setWorkFactor(12); return new \Phalcon\Security(); }, true);

$di->setShared('session', function(){ $session = new \Phalcon\Session\Adapter\Files(); $session->start(); return $session; });

$mailerConfig = [ 'driver' => 'mail', 'from' => [ 'email' => '[email protected]', 'name' => 'AltstreamFund' ] ];

$di->setShared('mailer', function() use ($mailerConfig){ return new \Phalcon\Ext\Mailer\Manager($mailerConfig); });

$di->setShared('component', function(){ $obj = new \stdClass(); $obj->helper = new Multiple\Frontend\Component\Helper(); return $obj; });

//Model Event Manager $di->set('modelsManager', function(){ return new Phalcon\Mvc\Model\Manager(); });

try{ //var_dump($di->getDefault()->get('cron')); $console->handle($arguments);

} catch (\Phalcon\Exception $ex) { echo $ex->getMessage(); exit(255); } ?>

MainTask.php

<?php

class MainTask extends Phalcon\Cli\Task{ //put your code here const ALREADY_PAIRED = 1, PROVIDED_PAIR = 2; const PENDING = "pending";

public function mainAction(){
    $resultStack    = [];
    $this->getDI()->getMailer()->createMessage()
        ->to("[email protected]")->subject("Registration Notification")
        ->content("Trying something")->send();

    $this->cron->runInBackground();
}

}

Well if it ran when you ran the command directly, then your code's probably fine.

If I recall correctly, CPanel has a way for you to get emails of any errors encountered when running cron jobs. Maybe it's a permissions issue? Or maybe the path to the PHP executable is different for the runner of cron jobs than it is for you.

ok. if i may ask if you were to point to the script that will run, how will you point to the cli.php taskname script from the cpanel cronjob..

Well if it ran when you ran the command directly, then your code's probably fine.

If I recall correctly, CPanel has a way for you to get emails of any errors encountered when running cron jobs. Maybe it's a permissions issue? Or maybe the path to the PHP executable is different for the runner of cron jobs than it is for you.

How you've done it is probably how I would do it. There's not really anything wrong with the command itself - as you know since you've been able to run it.

However, you've been able to run it as yourself. Cron jobs aren't typically run as the same user - ofter as root.

Frankly I don't think this is a Phalcon issue, or even an issue with your code. I would talk to your ISPs support to see if you can get any details or help with this cron job.

ok thanks.. really appreciate the time.. will keep this opened some

How you've done it is probably how I would do it. There's not really anything wrong with the command itself - as you know since you've been able to run it.

However, you've been able to run it as yourself. Cron jobs aren't typically run as the same user - ofter as root.

Frankly I don't think this is a Phalcon issue, or even an issue with your code. I would talk to your ISPs support to see if you can get any details or help with this cron job.