Ryan Dorn
Ryan Dorn

Reputation: 427

Run a PHP Cron Job on macOS & MAMP

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?

Upvotes: 2

Views: 1837

Answers (2)

CryptoRhino
CryptoRhino

Reputation: 27

The thing I like to do is:

  1. Make sure you have php installed using brew (brew install php@version (I use [email protected]))
  2. Check if you have crontab, else install using brew install crontab
  3. Enter crontab -l into the terminal, this will open a vim editor. If you can't seem to type anything press the i key. Then start entering the commands like : */1 * * * * cd /path/to/project; php index.php
  4. press : and following it enter wq and enter to save and exit the vim, making it :wq
  5. check cronjob using crontab -e in terminal

Additional details here(for crontab) and here(for vim)

Upvotes: 0

benvdh
benvdh

Reputation: 603

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

Upvotes: 1

Related Questions