319

I need a crontab syntax which should execute a specific PHP script /var/www/html/a.php every minute. The execution on every minute must start at 00:00. The other task which must execute a script at 00:00 /var/www/html/reset.php (once every 24 hours).

T Zengerink
  • 3,983
  • 5
  • 27
  • 31
Michael
  • 6,149
  • 14
  • 54
  • 91

2 Answers2

590

every minute:

* * * * * /path/to/php /var/www/html/a.php

every 24hours (every midnight):

0 0 * * * /path/to/php /var/www/html/reset.php

See this reference for how crontab works: http://adminschoice.com/crontab-quick-reference, and this handy tool to build cron jobx: http://www.htmlbasix.com/crontab.shtml

Eye
  • 7,902
  • 7
  • 40
  • 68
Jan Hančič
  • 49,796
  • 15
  • 87
  • 97
  • 4
    Dear Jan! Great answer. How about running a cron every 30 seconds? Is it like this? * * * * */30 /path/to/php /var/www/html/a.php ? – flaab Nov 26 '12 at 18:10
  • 27
    Unfortunately you can't run cron jobs more frequently than every minute. You'll have to use something else for that. – Jan Hančič Nov 26 '12 at 19:41
  • 9
    Jan Hančič, you can do this. You just need to use a simple trick described here: http://stackoverflow.com/a/1034304/1580615 – Ruben Aug 05 '13 at 03:28
  • Is it normal practice to execute .php script each minute with cron? Can it reduce server productivity? Is there any other bad side effects? – Andrew Feb 19 '16 at 08:54
  • @flaab for every 30 seconds you can try something like this: - * * * * * curl --silent URL >/dev/null 2>&1 * * * * * sleep 30; curl --silent URL >/dev/null 2>&1 – Shashank Shah Nov 28 '16 at 05:15
380

This is the format of /etc/crontab:

# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

I recommend copy & pasting that into the top of your crontab file so that you always have the reference handy. RedHat systems are setup that way by default.

To run something every minute:

* * * * * username /var/www/html/a.php

To run something at midnight of every day:

0 0 * * * username /var/www/html/reset.php

You can either include /usr/bin/php in the command to run, or you can make the php scripts directly executable:

chmod +x file.php

Start your php file with a shebang so that your shell knows which interpreter to use:

#!/usr/bin/php
<?php
// your code here
Greg
  • 9,849
  • 5
  • 26
  • 33
  • 42
    That's the format of `/etc/crontab`, which is a *system* crontab file. A *user* crontab has a different format, which doesn't include the username field, since it runs as the user who submitted it. If you want to run a cron job as a non-root user, you should use the `crontab` command to submit it (and not worry about where the crontab is stored). Don't mess around with `/etc/crontab` unless you really need to. – Keith Thompson Jul 18 '13 at 17:43
  • umm...nope, still not working – Madeo Feb 19 '20 at 07:18