24

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.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
djnetherton
  • 757
  • 1
  • 7
  • 19
  • are you running this script on windows or linux ? – Adidi Apr 22 '13 at 10:04
  • I believe the server is a linux one. – djnetherton Apr 22 '13 at 10:05
  • http://en.wikipedia.org/wiki/Cron `0 0 * * * /path/to/php /path/to/phpscript` 0min 0hour every day month year. php with full path on local server, php script with full path. OR `0 0 * * * wget http://example.com/scripts/scriptExample.php` – Waygood Apr 22 '13 at 10:11

4 Answers4

24

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.

karmafunk
  • 1,453
  • 12
  • 20
2

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
Adidi
  • 5,097
  • 4
  • 23
  • 30
2

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

Nishant
  • 3,614
  • 1
  • 20
  • 26
  • 2
    As both are 0. Its 0 mins, 0 hours to be precise, the order is: MINS HOURS DAYOFMONTH MONTH DAYOFWEEK #command# – Waygood Apr 22 '13 at 10:19
0

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.

Smita
  • 4,634
  • 2
  • 25
  • 32
Chris par
  • 65
  • 1
  • 8