Extending and Hacking ActionApps

From ActionApps Documentation
Revision as of 13:39, 28 July 2005 by Marek (talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Cron - run tasks regulary

Cron - how to automatically run tasks on given time?

The script cron.php3 - similar to Unix cron - is able to run PHP scripts on given time. If you want to send e-mail notifications if some condition is satisfied or if you want to exchange items between slices regularly, you will find it useful.

The reason for this script to exist is that it's not easy to run PHP scripts by the Unix cron. It also allows us to use admin interface (AA -> Misc - Cron) to add/modify new tasks to run.

If you want to run some task on given time (like running admin/xmlclient.php3 for Cross Server Item Exchange each 15 minutes), you have to do two steps:

  1. add new task on cron admin page (AA -> Misc - Cron)
  2. make you sure the cron.php3 is periodically started from unix cron

1. Add new task on cron admin page

The task you can set/edit on cron admin page (AA -> Misc - Cron). The fields (their names are taken from PHP getdate()) have the following meaning: Let's call time units all from: minutes, hours, mday (month day, 1-31), month, wday (week day, 0-7, where 7 and 0 both mean Sunday). You may use three-letter month and week day names (like "jan","feb","sun","mon"). A time unit may be set to:

* not set - jump over this field
10 number: run only when time unit equals
1-5 range: run when time unit equals 1 or 2 or 3 or 4 or 5
0-59/15 range with step: run when time unit (minutes in this case) equals 0 or 15 or 45
5,15-25/5,40 you may combine numbers, ranges and ranges with steps, separated by comma

  • Don't write any whitespace (spaces, tabs) into the time units.
  • Write script name into the "script" field. Use relative path from apc-aa home dir, e.g. "include/myscript.php3".
  • If you need to send any parameters to the script, write them into the "params" field in form "var1=value1&var2=value2".

The task table is allready prefilled on APC ActionApps setup, so you should see something like:

 time script params * 1 * * * misc/alerts/alerts.php3 howoften=daily&lang=en (Alerts - daily diggest) * 1 * * 1 misc/alerts/alerts.php3 howoften=weekly&lang=en (Alerts - weekly diggest) * 1 1 * * misc/alerts/alerts.php3 howoften=monthly&lang=en (Alerts - monthly diggest) * 1 * * * misc/alerts/admin_mails.php3 (Alerts - sending of subscription e-mails,...) * 8,23,38,53 * * * admin/xmlclient.php3 (CSN - Item Exchange) 
(it is just an example - the set of task could be changed in next release of ActionApps)

2. Start cron.php3 periodicaly from unix cron

This must be done by a system administrator (i.e. someone with root shell access), but only has to be done once no matter how many task are sheduled then.

In order we can shedule tasks from ActionApps we must run periodicaly the script cron.php3 (based in root apc-aa install directory). It is not so easy to start php scripts directly from unix cron, that's why you should use aa-http-request shell script (based in misc/ directory), which will start cron.php3 script for you.

The instructions for setup of the aa-http-request script is also inside the script. For setup follow the nex few steps:

  • For security reasons it is better to move the aa-http-request script to some directory, where webserver don't have access to. For example to the directory /usr/local/myscripts/aa-http-request
  • Open the aa_http_request script
  • Modify the HOST variable to point to your server where ActionApps are installed
    Example: HOST=aa.ecn.cz
  • Modify the AADIR variable to be filled by the path to root of AA instalation from server root
    Example: AADIR=/aaa (for AA installed on http://aa.ecn.cz/aaa)
  • Set LYNX or PERL to absolute path of lynx or perl if you have either of them. If neither are set, then the script will try telnet, which will not work if your server uses virtual hosts.
    Example:
    LYNX=/usr/bin/lynx
    PERL=/usr/bin/perl
  • Save the changes and close the script
  • In order to run this script from cron, it must have executable privileges
    ( chmod 755 aa-http-request )
  • Test it, by running aa-http-request. If you have already configured some task on AA -> Misc - Cron page (you probably have), then check if 'last_run' column is changed for sheduled tasks
  • Set the crontab so the script is started each 15 minutes e.g.
    crontab -e
    and put the line pointing at the aa-http-request:
    1,16,31,46 * * * * /usr/local/myscripts/aa-http-request > /dev/null