Rusty3 Posted September 5, 2009 Share Posted September 5, 2009 What's the best way of doing it? (with set_time_limit already to max) Say I want to keep it running for 4 or 5 hours. Running it from shell with "nohup nice"? Also, how to detect it stalled and restant it? Controling "ps" id on the shell? Quote Link to comment https://forums.phpfreaks.com/topic/173179-running-a-php-script-for-a-long-time/ Share on other sites More sharing options...
kratsg Posted September 5, 2009 Share Posted September 5, 2009 I'm just going to throw out a couple of questions. Is it going to be looping through some sort of array? What kind of functions are you going to be running? I'm just going to throw out a suggestion as this is something I wonder about myself: - perhaps using AJAX to repetitively call the php page - the php page would possibly use sessions to keep track of its progress - the AJAX function would use the results to determine the error or to continue Quote Link to comment https://forums.phpfreaks.com/topic/173179-running-a-php-script-for-a-long-time/#findComment-912889 Share on other sites More sharing options...
Rusty3 Posted September 5, 2009 Author Share Posted September 5, 2009 Thanks for the reply. I don't see how ajax could make a difference?! But,maybe, that's me. Yes, db, array, the usual stuff. Flushing it to keep sending output. I also wonder how to detect when to reload it. Sometimes php scripts stop responding and running but still show up as alive. Till they are killed... but then I would not be keeping it continuosly running.... Quote Link to comment https://forums.phpfreaks.com/topic/173179-running-a-php-script-for-a-long-time/#findComment-912899 Share on other sites More sharing options...
kratsg Posted September 5, 2009 Share Posted September 5, 2009 Well, the detecting when to reload it would be my idea of using AJAX. AFAIK, there's no way to determine how a PHP page is running (if at all) without checking it after each loop... unless you just check the database process calls instead (<-- this might be viable, if I knew how to check the processes running or how to determine whether that process call is from that specific script and not another script). If you had a cron job that would execute the PHP page and if it resulted in an error, write the error to a file. A separate cron job process would analyze this file every minute or so and if there is an error, reset the cron job? Quote Link to comment https://forums.phpfreaks.com/topic/173179-running-a-php-script-for-a-long-time/#findComment-912900 Share on other sites More sharing options...
bundyxc Posted September 5, 2009 Share Posted September 5, 2009 Before anybody mentions it, I'll explain why this won't work: <meta http-equiv="refresh" content="60"> PHP runs server-side, so seeing as how the refresh happens serverside.. it would time-out before the refresh could ever take place. What about something like this: $timeOut = $_SERVER['REQUEST_TIME'] + 59; while ($whatever) { if (time() == $timeOut) { die('<meta http-equiv="refresh" content="600">'); } else { //Do code } Quote Link to comment https://forums.phpfreaks.com/topic/173179-running-a-php-script-for-a-long-time/#findComment-912920 Share on other sites More sharing options...
Rusty3 Posted September 5, 2009 Author Share Posted September 5, 2009 The script triggering its own refresh! Very clever. Will try it out... Wait a minute, it won't cut the cheese! Instead of $timeOut, I tought about putting the refresh test on the main loop, but its eating its own tale. If script is dead, loop will not go on, refresh will not be triggered ... Quote Link to comment https://forums.phpfreaks.com/topic/173179-running-a-php-script-for-a-long-time/#findComment-912923 Share on other sites More sharing options...
Rusty3 Posted September 5, 2009 Author Share Posted September 5, 2009 kratsg: yes, I think there's a way of cron not running a script that its already running and if a cron is called every second, it would work. But its not an elegant solution... Quote Link to comment https://forums.phpfreaks.com/topic/173179-running-a-php-script-for-a-long-time/#findComment-912924 Share on other sites More sharing options...
Psycho Posted September 5, 2009 Share Posted September 5, 2009 If you would give a better explanation of what you are trying to achieve a suitable answer might be given. The AJAX idea could definitely be a viable one. I have some code to process thousands of folders with files int hem. Rather than do them all in one shot I have the script first "grab" all the folders and paths and store them in the db, then each successive AJAX call will process the next folder. Each time a folder completes the AJAX function on the client page is triggered and will send a another request that processes the next folder. This all continues indefinitely until the last folder is processed and an appropriate code is returned back to the page making the AJAX call and it then ends. Quote Link to comment https://forums.phpfreaks.com/topic/173179-running-a-php-script-for-a-long-time/#findComment-912927 Share on other sites More sharing options...
kratsg Posted September 5, 2009 Share Posted September 5, 2009 If you would give a better explanation of what you are trying to achieve a suitable answer might be given. The AJAX idea could definitely be a viable one. I have some code to process thousands of folders with files int hem. Rather than do them all in one shot I have the script first "grab" all the folders and paths and store them in the db, then each successive AJAX call will process the next folder. Each time a folder completes the AJAX function on the client page is triggered and will send a another request that processes the next folder. This all continues indefinitely until the last folder is processed and an appropriate code is returned back to the page making the AJAX call and it then ends. How do you run the AJAX code? Via a cron job or what? Quote Link to comment https://forums.phpfreaks.com/topic/173179-running-a-php-script-for-a-long-time/#findComment-912948 Share on other sites More sharing options...
Rusty3 Posted September 5, 2009 Author Share Posted September 5, 2009 If you would give a better explanation of what you are trying to achieve a suitable answer might be given. The AJAX idea could definitely be a viable one. I have some code to process thousands of folders with files int hem. Rather than do them all in one shot I have the script first "grab" all the folders and paths and store them in the db, then each successive AJAX call will process the next folder. Each time a folder completes the AJAX function on the client page is triggered and will send a another request that processes the next folder. This all continues indefinitely until the last folder is processed and an appropriate code is returned back to the page making the AJAX call and it then ends. How do you run the AJAX code? Via a cron job or what? Good question. Quote Link to comment https://forums.phpfreaks.com/topic/173179-running-a-php-script-for-a-long-time/#findComment-912951 Share on other sites More sharing options...
bundyxc Posted September 5, 2009 Share Posted September 5, 2009 Example: session_start(); $timer = 5; $refresh = $_SERVER['REQUEST_TIME'] + $timer; if (!isset($_SESSION['i'])) { $_SESSION['i'] = 0; } while (1) { if (time() >= $refresh) { die('<meta http-equiv="refresh" content="0">' . $_SESSION['i']); } else { $_SESSION['i']++; } } This script counts as high as it can, and refreshes every five seconds. After the refresh, it continues counting from where it left off. Any issues with my logic here? Obviously a five second refresh is unnecessary, but it's easier than waiting around for a full minute while testing the script. Quote Link to comment https://forums.phpfreaks.com/topic/173179-running-a-php-script-for-a-long-time/#findComment-912987 Share on other sites More sharing options...
Mark Baker Posted September 5, 2009 Share Posted September 5, 2009 kratsg: yes, I think there's a way of cron not running a script that its already running and if a cron is called every second, it would work. But its not an elegant solution...Cron tasks frequency can only be set to per minute. cron task should check for a lock file. Terminate if lock file exists If lock file not exist, create lock file ... run tasks clear lock file terminate Quote Link to comment https://forums.phpfreaks.com/topic/173179-running-a-php-script-for-a-long-time/#findComment-913028 Share on other sites More sharing options...
Rusty3 Posted September 5, 2009 Author Share Posted September 5, 2009 cron task should check for a lock file. Terminate if lock file exists If lock file not exist, create lock file ... run tasks clear lock file terminate Yes, that's the solution I was thinking about. A lock file or a lock key on the db. Cron would call a control php to do that, with a timeout of 59sec. Quote Link to comment https://forums.phpfreaks.com/topic/173179-running-a-php-script-for-a-long-time/#findComment-913172 Share on other sites More sharing options...
Psycho Posted September 5, 2009 Share Posted September 5, 2009 If you would give a better explanation of what you are trying to achieve a suitable answer might be given. The AJAX idea could definitely be a viable one. I have some code to process thousands of folders with files int hem. Rather than do them all in one shot I have the script first "grab" all the folders and paths and store them in the db, then each successive AJAX call will process the next folder. Each time a folder completes the AJAX function on the client page is triggered and will send a another request that processes the next folder. This all continues indefinitely until the last folder is processed and an appropriate code is returned back to the page making the AJAX call and it then ends. I run it by initiating the process via button on the web page. The process I built above was meant to be initiated by the user and not a nightly process. But, as I first stated, this would be a whole lot easier to help with if the end goal was better explained. How do you run the AJAX code? Via a cron job or what? Good question. Quote Link to comment https://forums.phpfreaks.com/topic/173179-running-a-php-script-for-a-long-time/#findComment-913187 Share on other sites More sharing options...
kratsg Posted September 5, 2009 Share Posted September 5, 2009 If you would give a better explanation of what you are trying to achieve a suitable answer might be given. The AJAX idea could definitely be a viable one. I have some code to process thousands of folders with files int hem. Rather than do them all in one shot I have the script first "grab" all the folders and paths and store them in the db, then each successive AJAX call will process the next folder. Each time a folder completes the AJAX function on the client page is triggered and will send a another request that processes the next folder. This all continues indefinitely until the last folder is processed and an appropriate code is returned back to the page making the AJAX call and it then ends. I run it by initiating the process via button on the web page. The process I built above was meant to be initiated by the user and not a nightly process. But, as I first stated, this would be a whole lot easier to help with if the end goal was better explained. How do you run the AJAX code? Via a cron job or what? Good question. Quote Link to comment https://forums.phpfreaks.com/topic/173179-running-a-php-script-for-a-long-time/#findComment-913344 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.