Page 1 of 1

cron job in php

Posted: Fri Sep 28, 2007 6:29 am
by vikasbatra
Hi friends,

i want to set up a cron job in php with php code, i don't have access to admin panel(control panel) of site.
so that's why i want to set up a cron job using php code. i already have some code related to cron job but that is not working, can any body have this type of code that can set cron job then pls mail me or send me links......



my mail id: vikas_batra06@yahoo.co.in, vikasbatra2006@gmail.com

Posted: Fri Sep 28, 2007 7:04 am
by anchises
Best bet would be to find a host who allows you to set up cron jobs.

Don't think it's really possible to manipulate cron itself using php. Not without system calls, anyway.

Failing that, I'd set up a small bit of code which checks, each time somebody requests a page, what the system time is and whether it needs to carry out a cron.

This means that you'll need to :
  • a) store the last time that the cron job was run (either in a database or file) - let's call it var_last_time;
    b) work out the next time that the cron job needs to run (for instance, the stored time plus 24 hours) - let's call it var_next_time;
    c) work out whether the current time is after var_next_time. If it isn't, don't do anything; if it is...
    d) ... run the cron job, store var_next_time as var_last_time;
    and carry on.
This works okay for regularly visited sites, but it gets more complicated if a number of periods elapse between a) and b) - you have to work out how many, make sure the cron job runs the right number of times, etc.

It also rather adds to the overheads of the scripts.

Posted: Fri Sep 28, 2007 7:06 am
by s.dot
Wow, I stopped development of my PHPCronEmulator because I thought there was no interest in it. :-P