Thursday, February 3, 2022

[FIXED] Run a PHP Cron Job on macOS & MAMP

Issue

I'm trying to run a cron job on macOS and a site hosted locally using MAMP.

I have tried various options, none to any avail; please see below:

*/1 * * * * php http://mylocalsite.com/cron/my_function

*/1 * * * * /usr/bin/curl –silent –compressed http://mylocalsite.com/cron/my_function

*/1 * * * * /usr/bin/curl --silent --compressed http://mylocalsite.com/cron/my_function

*/1 * * * * /Applications/MAMP/bin/php/php7.1.12/bin/php http://mylocalsite.com/cron/my_function > /dev/null 2>&1

*/1 * * * * wget --no-check-certificate -O- https://mylocalsite.com/cron/my_function >> /dev/null

When I run the following in terminal, it does work:

wget --no-check-certificate -O- https://mylocalsite.com/cron/my_function >> /dev/null

I know that the URL executes the function that I want as I have tested this directly in a browser.

What am I doing wrong and what should go into the crontab in order to ping/run the specified URL?


Solution

The CodeIgniter manual suggests that running CodeIgniter through the commandline is possible.

They write the following script:

<?php
class Tools extends CI_Controller {

        public function message($to = 'World')
        {
                echo "Hello {$to}!".PHP_EOL;
        }
}

In turn they execute the script like this on the server:

$ cd /path/to/project;
$ php index.php tools message

Where tools denotes the controller, and message the action.

So in that case the crontab entry would become:

*/1 * * * * cd /path/to/project; php index.php tools message

And in the case sketched in the question:

*/1 * * * * cd /path/to/project; php index.php cron my_function

SOURCE: https://www.codeigniter.com/user_guide/general/cli.html



Answered By - benvdh

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.