Run a PHP Cron Job on macOS & MAMP - macos

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?

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

The thing I like to do is:
Make sure you have php installed using brew (brew install php#version (I use php#7.4))
Check if you have crontab, else install using brew install crontab
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
press : and following it enter wq and enter to save and exit the vim, making it :wq
check cronjob using crontab -e in terminal
Additional details here(for crontab) and here(for vim)

Related

How to run a script from crontab and avoid "LoadError"

I want run my script from the crontab on Mac OS, but I'm getting an error:
ruby: Operation not permitted -- /Users/vitalii/Desktop/Home/update/update.rb (LoadError)
My preferences for the cron task and settings are created using rvm cron setup:
#sm start rvm
PATH="/Users/vitalii/.rvm/gems/ruby-2.4.1/bin:/Users/vitalii/.rvm/gems/ruby-2.4.1#global/bin:/Users/vitalii/.rvm/rubies/ruby-2.4.1/bin:/Users/vitalii/.rvm/gems/ruby-2.4.1/bin:/Users/vitalii/.rvm/gems/ruby-2.4.1#global/bin:/Users/vitalii/.rvm/rubies/ruby-2.4.1/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/Applications/Postgres.app/Contents/Versions/latest/bin:/Users/vitalii/.rvm/bin"
GEM_HOME='/Users/vitalii/.rvm/gems/ruby-2.4.1'
GEM_PATH='/Users/vitalii/.rvm/gems/ruby-2.4.1:/Users/vitalii/.rvm/gems/ruby-2.4.1#global'
MY_RUBY_HOME='/Users/vitalii/.rvm/rubies/ruby-2.4.1'
IRBRC='/Users/vitalii/.rvm/rubies/ruby-2.4.1/.irbrc'
RUBY_VERSION='ruby-2.4.1'
#sm end rvm
* * * * * ruby /Users/vitalii/Desktop/Home/update/update.rb >> /Users/vitalii/Desktop/logfile.txt 2>&1
I gave each file the rights to execute with chmod 777, but there are no changes and the error is repeated.
The contents of the file update.rb are
puts 'Hello, World!!!'
Can someone tell me what's going on and what I'm doing wrong ?
I found solution, since I use Mojave.
I need to make additional settings in the system. Who would have thought ...
enter link description here
This turns out to be a problem, since you need to allow permissions for cron.
And correct run command for crontab -e, this
* * * * * /bin/bash -l -c 'ruby /Users/vitalii/Desktop/Home/update/update.rb'
There is a little difference when setting crontab job via crontab -e or putting them in /etc/crontab
$ crontab -e
# m h dom mon dow command
* * * * * echo ok
$ nano /etc/crontab
# m h dom mon dow user command
* * * * * root echo ok
Is this the case?

Unable to run PHP Artisan command using Cron on Bitnami LAMP stack

I've created a custom Laravel PHP Artisan command which I was intending to use together with a cron job to carry out automated tasks on my server.
However, I'm having problems running the script and have tried a whole bunch of variations without much luck. Have looked high and low on the inter webs can't find anything to remedy my problems...
Here are a few of them:
* * * * * sudo su daemon -s /bin/sh -c "/opt/bitnami/php/bin/php /opt/bitnami/apps/demo/htdocs/ && php artisan schedule:run >> /tmp/output.txt 2>&1"
* * * * * cd /opt/bitnami/apps/demo/htdocs/ && php artisan schedule:run >> /tmp/output.txt 2>&1
* * * * * /opt/bitnami/php/bin/php /opt/bitnami/apps/demo/htdocs/ && php artisan schedule:run >> /tmp/output.txt 2>&1
The error I keep getting is: "/bin/sh: 1: php: not found"
I've also tried to execute the command as Bitnami, but no luck there either.
Thanks Jota, I ran the following and it seemed to have done the job:
* * * * * cd /opt/bitnami/apps/demo/htdocs/ && /opt/bitnami/php/bin/php artisan schedule:run >> /tmp/cron_output_8.txt 2>&1
Got this message instead now:
No scheduled commands are ready to run.
Which is good, I think that's just laravel, and I haven't set all that stuff up to run at this point.
Cheers,
Mikael

Docker and cron environments

I have problem with environment variables in docker image..
I created file: /cron.sh
#!/bin/bash
whoami
export
When I run manual in console: sh /cron.sh is OK (root, full envs) but when I add command to cron that I getting: root, basic envs (not have my variables).
Crontab -e:
* * * * * /bin/bash /cron.sh > /proc/$(cat /var/run/crond.pid)/fd/1 2>&1
Why are the differences?
I have solution..
before run "exec crond -n" in entrypoint I save environments to file.
export > /.env
Next in cron add:
* * * * * source /.env; /bin/bash /cron.sh > /proc/$(cat /var/run/crond.pid)/fd/1 2>&1
I hope I helped :-)

Implementing cron for Laravel is not working

I have Laravel 5 installed via composer on Ubuntu server.
My cron command looks like:
* * * * * /usr/bin/php /var/www/html/domain/public_html && php artisan schedule:run
And i get email from server with: "Could not open input file: artisan".
If i go to laravel directory, i can successfully run: php artisan command:command.
Cron is run as root.
If i remove "&&" from command, then nothing happens.
Any ideas, why it is not working?
please refer to this section in the docs
https://laravel.com/docs/5.6/scheduling#introduction
* * * * * /usr/bin/php /var/www/html/domain/public_html/artisan schedule:run >> /dev/null 2>&1
You should try this:
* * * * * php /var/www/html/yourprojectname/artisan schedule:run >> /dev/null 2>&1
For more details please follow this link
https://laravel.com/docs/5.6/scheduling#introduction or previous laravel version should tell you ... and this is my setting in cron * * * * * /usr/bin/php /var/www/html/domain/public_html/artisan schedule:run >> /dev/null 2>&1
I made a mistake in cron command.
This command works:
* * * * * /usr/bin/php /var/www/html/domain/public_html/artisan schedule:run

Running ruby selenium scripts on Cronjob - Chrome

I am trying to run a selenium ruby script on Chrome through crontab but don't see anything run.
I tried the following as suggested in other questions:
crontab -u shamanth -e
Added the following
MAILTO=""
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
export DISPLAY=:0 google-chrome
47 13 * * * /usr/bin/ruby ~/Desktop/script.rb
From the cron logs /var/log/syslog, I see that the script has started (I put some print messages in the beginning of the script) but I don't see chrome launching.
Permissions have been given to the script file.
Most likely the problem is with the path you are setting: crontab works in a very limited environment, where paths like ~ are not understood.
For that, instead of saying
* * * * * /usr/bin/ruby ~/Desktop/script.rb # NO!
# ^
say
* * * * * /usr/bin/ruby /home/your_user/Desktop/script.rb # YES!
# ^^^^^^^^^^^^^^^

Resources