I would like to run a PHP script every day at midnight. After research on how to do this, it appears that the best way to achieve this is to use a CRON job.
If my php script was located at http://example.com/scripts/scriptExample.php, can somebody be able to show the most simple example of what this CRON command would look like?
I have looked through numerous posts but I cannot find a simple enough example for me to learn and build upon.
Crontab needs the full path on your server.
0 0 * * * php /var/www/vhosts/domain.com/httpdocs/scripts/example.php
This will execute every day at midnight.
If You have a sudo access to your linux server :-
Then do the following
sudo crontab -e
This will open the cron tab for you on your server.
Next thing is you have to do a cron entry for the file which you want to execute
00 00 * * * /usr/local/bin/php "path of the php file which you want to execute"
00 00 * * *
this will run your cron at midnight daily, means at 0hrs and 0mins
So something like this:
00 * * * * /usr/local/bin/php /home/john/myscript.php
The 00 * * * * means hourly
/usr/local/bin/php - where php main engine is in
/home/john/myscript.php - the script to run (physical path)
You can use also @hourly special key:
@hourly /usr/local/bin/php /home/john/myscript.php
Are you using a company to host your website?
As you should have a icon in your c panel
called cron jobs
from there you can tell it what script to execute and when.