Setting up cron job to execute a PHP Script - php

I need to setup a cron job that'll execute a PHP script (used to send iPhone notification to users) every minute. I've tried several code snippets but none of them seem to work. Here's what I've tried:
crontab -e
then..in the editor, i typed
* * * * * php /Users/MYUSERNAME/Desktop/SimplePush/simplepush.php
but it doesn't work. I'm running on Mac OS X Lion and will be deploying the service on Linux service but i think the crontab ability shouldn't quite be affected by the OS as both are based on unix core. So, anyway, I need this cronjob to fire simplepush.php every minute to send notifications. How do I do it?

Have you tried running php /Users/MYUSERNAME/Desktop/SimplePush/simplepush.php in the terminal to check if it's working?

use this code :
* * * * * wget -q /Users/MYUSERNAME/Desktop/SimplePush/simplepush.php
or
* * * * * wget -o /Users/MYUSERNAME/Desktop/SimplePush/simplepush.php

I know this is an old question but this works for me in OSX Yosemite
*/1 * * * * php -q /Users/path/to/php-file.php

Related

Cronjob is not working on Ubuntu 16.04

I would like to put in place some cronjobs for my website.
My actual cronjob is:
4 * * * * /usr/bin/php /var/www/domain/backup.php
But it doesn't work.
Mannually the cron job is working.
With a service like https://www.easycron.com it's working too.
In Terminal, when I make crontab -e, I see my cronjob but it does not seem to run.
From where I can start my search to understand ?
Thanks.
In my server, when setting up a php file to run, I only have to put:
4 * * * * php /var/www/domain/backup.php. They run flawlessly. Works the same for my shell scripts as well with just bash /directory/thisScript.sh
I added this as an answer because my reputation score won't let me add comments yet.

Self Executing Script on timed schedule

I'm Working on a php app where I need to have a number count in a sql data base reset at the first of the month and at 12am everyday via PHP and MYSQL.
I have googled but found nothing. As far as I know PHP needs to be accessed by a client to be manipulated.
What is the best method of having the server do this on its own?
You can use crontab
* 0 * * * /path/to/my/script
You can use cronjobs. On windows 'scheduled tasks', on linux crontab. For linux:
crontab -e
A line have to look like this to be called every day 0 am:
* 0 * * * /bin/php -q /script/path
Or, to call the file via web but do not get the content:
* 0 * * * wget --spider "http://url.com/cron.php"
If you do not have a dedicated server, check your webspace control panel for this or ask your webhoster.

Running php script using cron on a Mac

What is wrong with this cronjob?
* * * * * * php -f /Documents/Programs/WeeklyHours/weekly_hour.php
I have combed through the various cron questions on StackExchange and nothing is working. When I run php -f /Documents/Programs/WeeklyHours/weekly_hour.php in the terminal it works perfectly. I know the cron jobs are running because I am getting error mail. At the bottom of the error mail messages it is saying "/bin/sh: Applications: command not found." Any ideas on what I am doing wrong?
Thanks in advance.
You have one more * than required in your crontab entry
Try
0-59 * * * * php -f /Documents/Programs/WeeklyHours/weekly_hour.php
The 0-59 is so that it will run every minute
The cron job likely runs under a different user which does not have its PATH set up the same as you, so it cannot find the php executable. You are able to simply type php because your PATH variable is set up to include its parent directory; that is not necessarily true for all other users.
Explicitly specify the path to the executable, e.g. /usr/bin/php. To figure out which php you're using, type:
$ which php
* * * * * * /usr/local/bin/php -f /Documents/Programs/WeeklyHours/weekly_hour.php >> /ww/xx.log 2>&1
You can view the log.

Cron not executing PHP script

Cron is running but for some reason it's not executing the script. I have checked to see if the script works and it does. I am trying to make the PHP script execute every minute.
* * * * * /etc/php5 /var/www/cron/automatedScript.php
The server is Linux, Ubuntu distro.
You are most likely giving it the wrong php executable path :)
On MY system it would be:
* * * * * /usr/bin/php /var/www/cron/automatedScript.php
To confirm the correct path to use execute:
nm#vp:~$ which php
And this will return you the path similar to:
/usr/bin/php
I think this may solve your problem
* * * * * /usr/bin/php -q /var/www/cron/automatedScript.php
Info : PHP path may be vary in different os. So you have to know right installtion path of PHP.

Trying to get php to run on cron

This is driving me a little insane, and I've gone through a hundered different things without touching on the solution; so I may miss out on some details on what I've done so far.
I'm trying to get a Cron job to run on my linux server ive got running in a datacentre. All I'm trying to get to run is a simple php script in the format:
* * * * * php -q /path/to/script/file.php
The php part runs fine if I type it in manually, but nothing happens when the cron runs; it also appears to run in the logs just fine, with no errors.
If i go back and edit with crontab -e, and put in the line
* * * * * echo "test" > /tmp/test.txt
That seems to work ok, it creates the text file.
Has anyone had any problems running a php script in this format?
(Btw I'm just testing with the run every minute, it doesnt work at any time.)
Any help is appreciated.
try invoking php with it's full path, for example /usr/bin/php
the cron will not have the same environment variables as your user profile have, so it might not find the executable.
Try to put full path to php binary (/usr/bin/php or similar)
I also don't have '-q' flag in my distribution. Check it.
Might just be some path craziness: I'd run which php and then copy the full path into cron. On one of my boxes it is /usr/bin/php and so you'd have:
* * * * * /usr/bin/php -q /path/to/script/file.php
Try that and see if it helps.
Perhaps your PHP binary is not in your PATH. Try using the full path:
* * * * * /path/to/php -q /path/to/script/file.php
It's possible that you may need to provide the full path to your php binary
* * * * * /usr/bin/php -q /path/to/script/file.php

Categories