Scheduling php scripts

I want to create some function for php script schedule, for example, if I want tu to run page.php on 12/12/2012 12:12, I can call

schedule_script('12/12/2012 12:12','page.php');//or by passing a time/datetime object 

or, for example, call a script every minute

 schedule_interval(60,'page.php');//every 60s=1minute 

I can add another function to see what the script is scheduled for or delete one of them.

I want these functions to work on both UNIX and WINDOWS platforms, I DO NOT want ugly solutions like executing a script on every page of the site (I want to schedule these commands when no one is on the site), or using "buisy wait "(using sleep () on a script that checks whether there are scheduled tasks) or something that requires user intervention (for example, write something on the console or open the panel).

I found the "AT" command in MSDOS (works well in all windows), but it is very simple because it only accepts time, not dates, there is a more powerful version on UNIX, but I don’t know how to use it (and I I want a solution for both platforms).

+3
source share
3 answers

There is a PHP function that allows you to defer script execution to a point in time.

So let's say I have cron.php :

 <?php // Usage: // cron.php [interval|schedule] [script] [interval|stamp] if(!isset($argc) || count($argc)!=2)die; // security precaution $time=(int)$argv[3]; // just in case :) if($argv[1]=='schedule'){ time_sleep_until((int)$_GET['until']); include_once($time); }elseif($argv[1]=='interval') while(true){ // this is actually an infinite loop (you didn't ask for an "until" date? can be arranged tho) usleep($time*1000); // earlier I said milliseconds: 1000msec is 1s, but this func is for microseconds: 1s = 1000000us include_once($argv[2]); } ?> 

And your classes / functions file:

 // Const form K2F - Are we on windows? define('ISWIN', strpos(strtolower(php_uname()),'win')!==false && strpos(strtolower(php_uname()),'darwin')===false ); // Function from K2F - runs a shell command without waiting (works on all OSes) function run($cmd){ ISWIN ? pclose(popen('start /B '.$cmd,'r')) : exec($cmd.' > /dev/null &'); } script_schedule($script,$time){ if(is_string($time))$time=strtotime($time); run('php -f -- schedule '.escapeshellarg($script).' '.$time); } script_interval($script,$mseconds){ run('php -f -- interval '.escapeshellarg($script).' '.$mseconds); } 

It should work. By the way, K2F is this structure that makes your dreams come true .;). Greetings.

Edit: If you still need parts related to counting running tasks and / or deleting (terminating) them, I can also help you with this. Just reply to my post and we will continue.

+2
source
 $amt_time = "1"; $incr_time = "day"; $date = ""; $now= ''. date('Ym-d') .""; if (($amt_time!=='0') && ($incr_time!=='0')) { $date = strtotime(date("Ymd".strtotime($date))."+$amt_time $incr_time"); $startdate=''.date('Ym-d',$date) .""; } else { $startdate="0000-00-00"; } if ($now == $startdate) { include ('file.php'); } 

Just guess;) Actually, I could have it in the reverse order, but you get the idea

0
source

This is my implementation of the scheduler, I only need to run it if it is inactive and add all the tasks to the mysql task table (I already have one for my main script), this script will run all the tasks ready for execution (the sql table has a datetime field )

What I call "Mutex" is a class that reports that one or more copies of the script are working, and can even send commands to the running script through the pipe (you just need to create a new mutex with the same name for all scripts), so you can even stop the script from starting from another script.

 <?php //---logging--- $logfile = dirname(dirname(__FILE__)).'/scheduler.log'; $ob_file = fopen($logfile,'a'); function ob_file_callback($buffer) { global $ob_file; fwrite($ob_file,$buffer); } //--includes--- $inc=dirname(dirname(__FILE__)).'/.include/'; require_once($inc.'Mutex.php'); require_once($inc.'jobdb.php'); //--mutex--- //i call it mutex but it nothing like POSIX mutex,it a way to synchronyze scripts $m=new Mutex('jscheduler'); if(!$m->lock())//if this script is already running exit();//only one scheduler at time //---check loop--- set_time_limit(-1);//remove script time limit for(;;){ ob_start('ob_file_callback');//logging $j=jobdb_get_ready_jobs(true);//gets all ready jobs,works with mysql if($j!=null)//found some jobs foreach($j as $val){//run all jobs $ex='*SCRIPT NAME AND PARAMETERS HERE*'; if(!run_script($ex)) echo "UNABLE TO LAUNCH THE JOB!\n"; } $n=($j!=null)?count($j).'JOBS LAUNCHED':'NO JOBS'; sleep(60); if($m->has_to_stop())//yeah,i can stop this script from other scripts,it works with a file pipeline exit("# STOPPING SCHEDULER\n"); ob_end_flush();//LOGGING } ?> 

My run_script function works the same as the Sciberras run function.

To activate the scheduler, you just have to use this command

  run_script('scheduler.php'); 

to check if it is active

 $m=new Mutex('jscheduler'); if(!$m->test_lock()) echo 'SCHEDULER IS ACTIVE'; else echo 'SCHEDULER IS INACTIVE'; 

and stop the scheduler

 $m=new Mutex('jscheduler'); $m->ask_to_stop();//simply sent throught the pipe the command,now has_to_stop()will return true on the process that have gain the lock of the mutex echo 'STOPPING SCHEDULER...'; 

Perhaps I have moved too far in its implementation, one way or another, there may be a problem with a β€œdelay”, for example, if the scheduler starts at 0: 00.00, and I have two scripts at 0: 01.01 and 0: 01.59, both are running in 0: 02.0. To correct this β€œlag”, I can return all tasks scheduled in the next minute and schedule them, as in Sciberras code, using time_sleep_until. This will not create too many running threads (I might want to check if there is a limit or performance drop when starting the number of HUDGE threads, but I'm sure there will be some problems) and provides an ideal time, requiring only checking, the scheduler is active.

0
source

Source: https://habr.com/ru/post/1398670/


All Articles