I had found a script that supposedly schedules a script to execute. I am very new to PHP and I do NOT want to use cron. Here’s the script I found:
<?php
$hour = date('G');
$day = date('N'); // 1..7 for Monday to Sunday
if (($hour >= 5 && $hour <= 7) // 5am - 7am
|| ($hour >= 10 && $hour <= 12) // 10am - 12 noon
|| ($hour >= 15 && $hour <= 19) // 3pm - 7pm
|| ($day == 5) // Friday
) { ?>
//my script
<?php } ?>
Because I know so little, If I put my script where it says, would it execute at that certain time every time that happens? Thank you in advance! -Ben
Better way is to use cron. This is quite simple, although it may look intimidating at first:
Step 1 – Create a script that will be run by cron
For example, place this in
<your-app-root>/cron.php:Step 2 – Add crontab entry
To automatically execute your
cron.phpscript, you can add a line like this to/etc/crontab:..where
some-random-value-heremust match the random value placed in the PHP script. This is a security feature to prevent anybody from running your cron code.Crontab line description
The first five parts of crontab line are: minutes, hours, day of month, month, day of week.
You can also use ranges, for example to run the script each day at 5, 7, 8, 12, 13, 14, 15, 20 you can use this:
You can also use “steps”, for example to run every 5 minutes (suggested for recurrent jobs, such as indexing / cleanup tasks, etc):
The sixth argument is the user that will be used to execute the command. You can use whichever user you want here, www-data, nobody, your user, etc. Best practice is to never use root here, unless really needed for some reason.
The remaining part of the line is the command that will be run at scheduled time.
The
&> /tmp/my-app-cron.logpart will make all the output from your latest cron.php execution to be stored inside/tmp/my-app-cron.log.Read more..
For more information on cron usage, you can refer to
crontab(5):