PHP pseudo-cron

Pseudo-cron uses a syntax very much like the Unix cron’s one. For an overview of the syntax used, see a page of the UNIXGEEKS. The syntax pseudo-cron uses is different from the one described on that page in the following points:

  • you may not use abbreviations (Mon, Tue, …) for day of week new in v1.1!
  • there is no user column
  • the executed command has to be an include()able file (which may contain further PHP code)

All job definitions are made in a text file on the server with a user-definable name (usually crontab.txt). Here is an example of a valid crontab file:


#comments start with '#'
#mi  h    d    m    dow      job                       comment
0    5    *    *    Sun      cronjobs/dump.inc.php     # make db dump every sunday at 5 am
40   5    2    *    *        cronjobs/sendlog.inc.php  # send last month's web server logs
*/15 8-19 *    *    Mon-Fri  cronjobs/refr_ext.inc.php # refresh external news sources

The last line will call cronjobs/refr_ext.inc.php every 15 minutes between 8am and 7pm on Monday to Friday.

Features: runs any PHP script
periodical or time-controlled script execution
logs all executed jobs
can be run from an IMG tag in an HTML page
follows Unix cron syntax for crontabs
sends an email with the results
Usage: Modify the variables in the config section below to match your server.
Write a PHP script that does the job you want to be run regularly. Be sure that any paths in it are relative to the script that include()s pseudo-cron.
Set up your crontab file with your script
Include pseudo-cron.inc.php in a high traffic page
Wait for the next scheduled run 🙂
Note: You can log messages from your job script to pseudo-cron’s log file by calling logMessage("log a message")

Version history:

v1.3 15.06.04
added: the number of jobs run during one call of pseudocron can now be limited.
added: additional script to call pseudocron from an HTML img tag
improved storage of job run times
fixed a bug with jobs marked as run although they did not complete
v1.2.2 17.01.04
added: send an email for each completed job
improved: easier cron job configuration (relative to pseudo-cron, not to calling script. Please read the release notes in the source.
v1.2.1 03.02.03
fixed: jobs may be run too often under certain conditions
added: global debug switch
v1.2 31.01.03
added: more documentation
changed: log file should now be easier to use
changed: log file name
v1.1 29.01.03
changed: renamed pseudo-cron.php to pseudo-cron.inc.php
fixed: comments at the end of a line don’t work
fixed: empty lines in crontab file create nonsense jobs
changed: log file grows big very quickly
changed: included config file in main file to avoid directory confusion
added: day of week abbreviations may now be used (three letters, english)
v1.0 initial release

Download

Leave a comment