How to create cron jobs on cakephp 4

Can you please help me to create a server side cron job on cakephp 4

Unless I have misunderstood the question, a cron job is added via your SSH login (or on your cpanel). It has nothing to do with Cake. A cron job is a Linux specific scheduled task; it is to say one level higher than your webpage. Its a command you execute on the server which hosts your webpage.

So your server could be windows or linux or other, and your webpage could be cake or drupal or wordpress or whatever. Like, your computer runs Windows (probably), but you wouldn’t ask “What do I change in Word so that I can have a different desktop wallpaper?” - bad analogy I know, but it’s late at night!

You may have more success here asking what you want your cron job to do? Odds are it may be possible from off your website - even if not recommended (like, you could use PHP to make a SQL backup and download it - but I wouldn’t!).

Sorry if I have misunderstood your question though.



Thanks for reply.
I have created everything on my cPanel for run a cron job.
My cakePHP version is 4.x

I have a controller and I added a function there. I want to call this function using cron job.

I have created a file inside webroot named cron_despatcher.php
Here is the code:

<?php $_SERVER[ 'HTTP_HOST' ] = ''; // use only domain name here require dirname(__DIR__) . '/config/bootstrap.php'; use Cake\Network\Request; use Cake\Network\Response; use Cake\Routing\Router; use Cake\Routing\DispatcherFactory; if(PHP_SAPI == "cli" && $argc == 2) { $dispatcher = DispatcherFactory::create(); $request = new Request($argv[1]); $request = $request->addParams( Router::parse( $request->url, '' ) ); $dispatcher->dispatch( $request, new Response() ); } else { exit; } ?>

My controller code is here:

<?php namespace App\Controller; use App\Controller\AppController; class CornsController extends AppController{ public function initialize(): void { parent::initialize(); $this->loadModel("Corns"); $this->layout = false; } function addCorn() { $data = array(); $check_num = rand(); $data['check_num'] = $check_num; $corns = $this->Corns->newEmptyEntity(); $corn = $this->Corns->patchEntity($corns, $data); $this->Corns->save($corn); } } ?>

Cron Command:
/usr/local/bin/php /home/atgo/public_html/webroot/cron_dispatcher.php /Corns/addCorn

But not working.

Can you please check that if I did wrong anything!!!

I could make a couple of guesses, but its crossing my skill level here!
It may be getting unstuck on any authorizations or policies you may be using.
Its possible the cron can’t replicate a fully working browser to do the fetch, maybe you could have it run a perl script which can?
Your telling the job to use the absolute path, so maybe its getting lost in the depth of the tree, and may need to be referenced via a www. kind of thing.
(As your using Cli those last couple probably may not relevant)

I would guess you’re just wrapping a couple of SQL commands there - I don’t suppose you could put those in a bash script and just do the direct SQL stuff without the PHP front-end.

But that’s it for me, really need a better person to help further here sorry!

You should ideally not be writing this as a controller, but as a command. They are expressly meant to be called from the command line.

Hi, thank for reply.

But I need to add a record using cron job.

Can you please help me.

Yeah, you write a command that can add a record, and then you call that command using a cron job.

Can you please share me the details!

The link I gave above includes sections on how to write console commands and running shells as cron jobs. They will be far more comprehensive than anything I might write for you here.

Hi, thanks for reply.
My cakePHP version is 4.x. This command is showing version 3.x. This will work?

Sorry, updated the links to v4 of the book.