Crontab - e -> Using cron to run scripts at set times
Crontab is the way on *nix servers to run scripts at preset times. The format can be a little weird to get use to so I poked around the web looking for some sort of builder or generator to help me generate cron entries so that I wouldn't have to memorize the format of the file. Google normally produces really relevant results, but in this case I couldn't find (easily, anyway) one that anybody else had created. So I decided to write my own. Here's the format of cron for reference:
minute(s) hour(s) day(s) month(s) weekday(s) command(s)
so the following would run program.cgi at 10 minutes past the hour EVERY hour of the day
10 * * * * /path/to/program.cgi
What if you wanted to run the program.cgi every 10 minutes of every hour? You would format like this
00,10,20,30,40,50 * * * * /path/to/program.cgi
in the case of PHP if you don't have PHP installed as a CGI then you can use lynx to call it. Here is an example of running a script at 3am every day via cron using lynx:
0 3 * * * lynx -dump http://www.yourdomain.com/your_script_to_cron.php
You probably already have lynx installed if you are on a unix/linux server but ask your host if you aren't sure about it.
I realized as I started to go through this that this is something I've done time and time again using scripting: taken a series of manual steps and reduced them into scripts. So I will explain the steps to do this here and maybe you can include in automating some of your tasks.
Here's
the "simple" submit form I put together to do this:
|
Run at X Minutes: |
OR (separate
multiple by comma) Example: Run in January only |
|
Run at X hours: |
OR (separate
multiple by commas) Example: Run in January only |
|
Run at X days: |
OR (separate
multiple by commas) Example: Run in January only |
|
Run at X months: |
OR (separate
multiple by commas) Example: Run in January only |
|
Run at X weekdays: |
OR (separate
multiple by commas) Example: Run on mondays only |
|
Script/program to execute: |
must be full path |
How to dynamically generate options in a select menu
Note: the number ranges for the dropdown menus are missing from the form above. I wrote a handy little function to populate dropdown menus for me. The function looks like this:
function fillDD($min, $max, $selected) {
for($i=$min; $i<$max+1; $i++) {
if($i == $selected) {
print("<option SELECTED
value=\"$i\">$i</option>\n");
} else {
print("<option
value=\"$i\">$i</option>\n");
}
}
}
All I have to do is pass to the fillDD() function the minimum, maximum and selected values like this:
<select size="1"
name="minutes">
fillDD(0, 59, 0);
</select>
Example 36. Creating a function to build dropdown select menus - source
If you'd like to use the actual cron generator script to build cron entries that I created for use, feel free to do so. The utility is part of my webmaster utilities page at TD Scripts and here is the cron generator
It will also give you vi editor code to edit the cron, which is useful.
Happy coding to you!
==============
New forum for discussion of diary entries
The homeroom at Script School is available to discuss this diary entry. You must be an enrolled student at Script School to add comments to these diary entries.
Please vote on the usefulness of this diary entry so other people will know if it is worth their time to read :)
[back]
12/30/02 "Creating cron
entries, dynamic select option results"
[next]
Home: PHP Diary | Script School | PHP Scripts | TD Scripts.com
Copyright 1999-2003 php-scripts Last Modified 02/9/04 11:31