View Full Version : Tutorial: Schedualed tasks.

11th February 2006, 07:54 PM
This Tutorial presumes you run a linux webserver, have access to the shell and know how to write at least basic php.

So meny people have asked me how the turns work on fallofempires so i thought i'd share the secret with everyone. The secret is simple, within linux and other similar operating systems you have something known as a crontab which will run events based on the times you specify. Aditionally some web based server management programs such as control pannel and direct admin offer a more friendly way to edit the cron.

There are two methods of running tasks that are used in fallofempires, the first is used where we only need to execute sql queries (for example turns and ranks) the second is used when more complex things need to run (for example the online users updater).

Method one.

write your sql queries ensuring each is terminated with a semi colon then save them into a .sql file outside of your web directory. The first line of this file should contain 'Connect yourdatabasename;'

log into the shell, or admin pannel and open up the crontab editor (in the shell you need to type crontab -e) For each set of sql statments you wish to run you will need a line that looks something like this:

10 * * * * mysql -uyourusername -pyourpassword < /path/to/script/turn.sql

pressing ctrl + o then enter will save the file, pressing ctrl + x will exit.

For running something like turns in a game, the only * you will be interested in is the first one, which is the minuites past the hour that it runs on. The example above runs at 10 mins past every hour. (if you wanted to run every 30 mins you could write 00,30 * * * * instead of repeting it twice) For more info on what the *'s represent and what you can put in their place see: http://www.adminschoice.com/docs/crontab.htm

Method 2.

For the more complicated things that require php first write your php script then as the top line add this: '#! /path/to/php/php' (to find the path you can use the command 'whereis php' in the shell) Then you need to make the file executable with the chmod command (chmod 744 filename) some ftp programs will also allow you to make this change.

after you have chmodded the file you can add it as a line on its own in the crontab this time your line may look something like this:

20,40,60 * * * * /path/to/script/advancedphpscript.php

ok, i hope that this has at least given you somewhere to start from and bear in mind it was late at night when i wrote this so there may be some mistakes.

11th February 2006, 09:19 PM
For those of you who are curious, the equivalent program in windows is called scheduled tasks... you should be able to find it in the control panel.

To have it repeat tasks and some other specifics, you'll have to open up the properties after you have created your task; you can find repeat settings by pressing the advanced button on the schedule tab.