Page 1 of 1

cron & php

Posted: Tue Sep 03, 2002 10:02 pm
by m3mn0n
I dont know if this is the proper forum for this or not so here goes anyway...


For this turn-based webgame i am the admin of you need to enable the file that executes turns to run every so often (as much as you want turns to be distributed)....

It was suggested to me to use crontab to set this up, my webhost has it enabled, but i think maybe i am typing somthing in wrong....


Image

...all that was before turns.php was the URL for the file, no commands...

I also did a search for it, and i got nothing good that explains the setup, but it did say somewhere you need to add something else into the cron to run php scripts....

does anyone got any links to tutorials or if you have experience, do you mind explaining what to do?

thanks alot! :D

Posted: Tue Sep 03, 2002 11:04 pm
by volka
i.e. http://g.unsa.edu.ar:6711/man?=crontab&=5. just googled for "man cron" or "man crontab"

Your settings will launch <command> once an hour, at 25 minutes after the full hour. Probably you want the script to be executed every 25 minutes.

I will try to explain the usage of crontab and also why 25 minutes of all step widths is -hmmm, let's say- tricky.

Let's start with the minute field of an crontab entry - all others stay *.
You entered 25. This causes cron to run the script every time the current time's minute-field equals 25 (as mentioned above: once an hour).
You may also enter a comma-separated list of 'matches', i.e. 0,10,20,30,40, 50 this will execute the script every 10 minutes (every time the current time's minute field is equal to one of the entries in the list).
You can enter a range using <start>-<stop>, i.e. 0-59 will run the script every minute. In conjunction with a step width parameter you can limit the execution, i.e. 0-59/10 will again execute the command every 10 minutes (matches start at 0 step width 10). Since the minute field can only contain values from 0 to 59 this is the same as */10.

All other fields in a crontab entry behave the same way.
The command is executed only if for all fields a match is found.
a single * means "doesn't matter"
i.e.
# minute, hour, day of month, month, day of week
*/10 */4 * 2-3 * doSomething
will launch doSomething every 10 minutes on every fourth hour in february and march.

ok, back to your 25 minutes.
60 (minutes of an hour) cannot be devided by 25 without remainder, so you can't find an expression that work on the minutes field alone.
You have to add several crontab entries (*), all execute the same script but on other conditions.
Your first entry will start at 0 minutes - next execution shall be 25 minutes later and so on. This leads to 0,25,50 for the first entry.
Next call must be on 15 (0:50 + 25 minutes) -> 15, 40 and so on.
Unfortunately the first number that can be devided by 25 AND 60 is 300.
300 minutes, that's 5 hours! Not until five different entries your 'minutes'-condition can start over again.
  • minute field's conditions:
  • 0, 25, 50
  • 15, 40
  • 5, 30, 55
  • 20, 45
  • 10, 35
But it's going on: you can't divide 24 (hours a day) by 5 without remainder.
Unfortunately again, 5 is prime so the least common multiple (is this the correct term?) must be 24*5
...and so on...

If you're lucky I missed something in the syntax of crontab or a way of simplifying the conditions ;)


edit: (*) just as an example:
a script shall be executed every 40 minutes.
can't divide 60 by 40 without remainder, but 120 --> 2 entries.
On even hours excute the script at 0 and 40 minutes after the full hour.
On odd hours just at 20 minutes after the full hour -->
# minute, hour, day of month, month, day of week
0,40 0-23/2 * * * doSomething
20 1-23/2 * * * doSomething

Posted: Wed Sep 04, 2002 2:38 am
by sunderwind
Can anyone clarify does the linux cron notation of */25 in the minutes section mean every 25 minutes or does it mean 25 and 50 minutes?

Posted: Wed Sep 04, 2002 3:03 am
by volka
0, 25 and 50
starting at 0 (*) step width 25 -> 0+(0*25), 0+(1*25), 0+(2*25) [0+(3*25) > 59 --> stop ]

Posted: Wed Sep 04, 2002 4:50 am
by Wayne

Code: Select all

/usr/bin/lynx -dump http://......../turns.php

Posted: Wed Sep 04, 2002 5:39 am
by volka
or just
/usr/bin/php /path/to/turns.php

Posted: Wed Sep 04, 2002 6:22 am
by gite_ashish
For php 4.2.2,
/usr/bin/php -f /path/to/turns.php
The 4.2.2 requires either -f or -q options, to suppress HTTP header output.

For crontab the syntax is:

<min> <hr> <month day> <month> <week day> <user> <command>

I tried without the <user> field and crontab don't execute the script. The script will execute properly with the proper <user> field.

So,
* * * * * root /usr/bin/php -f /path/to/turns.php
* * * * * nobody /usr/bin/php -f /path/to/turns.php
will run every minute !

i m running red hat linux 6.2, php 4.2.2, crontabs-1.7-7, vixie-cron-3.0.1-40.

Posted: Wed Sep 04, 2002 7:04 am
by m3mn0n
um, thanks alot for the explanation, i understand the how the numbers are setup and work now, but i am still getting errors with cron...

With my webmail i get this:
Subject: Cron <x@server3> http://x.x.x/x/x/x/turns.php All headers
/bin/sh: http://x.x.x/x/x/x/turns.php: No such file or directory
my cgi-bin is located i folder up from the turns script, anyone have a clue why it's displaying "/bin/sh" ?

Posted: Wed Sep 04, 2002 7:06 am
by m3mn0n
BTW, I'm getting that error every 10 mins (just how i wanted cron to run). So i know i'm timing it correctly, but now the problem is something else...

Posted: Wed Sep 04, 2002 11:06 am
by sunderwind
/bin/sh: http://x.x.x/x/x/x/turns.php: No such file or directory
cron runs an executable file on the file system /path/to/exacutable. If you are running php as a cgi then you can use cron to run the file directly.
example: * * * * * /bin/sh /path/to/file.php
Note: you must have #!/path/to/php as the first line of the file and the file must have the executable bit set.

Otherwise,

If you need to have cron make a call to a web server consider using CURL. It is a small web client that executes from the command line and returns the web results on standard out. It can handle SSL, FTP, Gopher ....
example: * * * * * /path/to/curl http://server.name/web/path/to/turn.php

Another solution to solve the problem with cron's time limitations is to have cron call the file every minute. Let PHP figure out when to execute the code based on a timestamp on a file or in a database.

or

My preferred method is to write a daemon that runs forever and sleeps for a specified amount of time. You can make it so that whenever a web request is honored on your other php scripts, they check to make sure the daemon is running.

Code: Select all

#!/path/to/php
&lt;?php
//This should be run as a cgi, so that it will not tie up a web server process
define("DAEMON_WAIT_MINUTES",25);

while(! $daemon_finished){
  // Give the code within this while loop 1 minute to parse per pass.
  set_time_limit((DAEMON_WAIT_MINUTES + 1 ) * 60);

  // Do the guts of each pass.
  print("Do Stuff");

  // Tell the daemon to wait for specified number of minutes before it does
  // the while loop again.
  sleep(DAEMON_WAIT_MINUTES * 60);
}
?&gt;
You will need to add some checking into the code, ie storing a process id in a file or DB, so that you can make sure the daemon is running....

I personally use this on a site that generates auto email alerts when a entry is save that matches a users auto alert criteria. The web script puts the match in a queue where the daemon picks it up and goes through each one. How long it takes to complete really does not matter, since it is not tying up a web process.

Hope this helps.

Posted: Thu Dec 16, 2004 6:20 am
by lazerbri
Sorry but how can i find the path/to/php

Posted: Thu Dec 16, 2004 7:01 am
by timvw
timvw@localhost:~$ whereis php
php: /usr/share/php