Run a script every 5 seconds?

Good evening,prompt please how to write a function or piece of code that will be executed every 5 seconds, no cron tasks in the database
June 8th 19 at 16:55
4 answers
June 8th 19 at 16:57
Create this code and console start.
while(true) {

// some code

sleep(5);
}


But there are two that I know of caveat - once you close the console, the script will stop working (solved by using the so-called screen screen). And the second server may be a time limit on script execution. On shared hosting it is usually not more than 120 seconds.

And another point - if the script is not optimized, it will pagerate memory, until it ends and the OS it won't.

Or alternatively, you can use a PM2 for NodeJS - there in the background to keep the process, do not need crowns and dB, but we need NodeJS.
eventloop, queue? - jaycee commented on June 8th 19 at 17:00
1. It's pretty efficient to just wait in addition, the script execution time !=0, so it's not the 5C.
2. No additional screen and things like that don't need it now - you simply run this script using systemd. But shared hosting is not poluchitsa, no start screen, no systemd to do something, if it comes to that...
3. Time limit for cli is not available by default. And on shared hosting, in few places, in General, you can run something through php-cli
4. And, of course, where you would start to nod, for these purposes, you can safely run php script as described in section 2, so that the meaning of this "brilliant" step just yet. - Guiseppe commented on June 8th 19 at 17:03
June 8th 19 at 16:59
To Use Cron
Kron does not know how such discreteness to run. 1 minute minimum. - jaycee commented on June 8th 19 at 17:02
he can , I was running every 20 seconds. You just slip in a command to add, well, spawn 12 teams with different value of the slip, but in General feasible. Though the solution looks not very. - Guiseppe commented on June 8th 19 at 17:05
Himself crowns can not. You can write some wrapper, but the conversation was not about that. About your options with the slip I mentioned above also said... - Guiseppe commented on June 8th 19 at 17:08
it is clear that this solution is not optimal, but the conditions is also incomplete, unclear around what is available. Personally, I would run the script type while(true) screenshot (here the disadvantage that the screens die sometimes and not start after a reboot of the system) or held in the PM2 process, because systemd didn't work.

About the same order that the script can run longer than 5 seconds - it can be sent in background with the function fastcgi_finish_request() (assuming that a web server is NGINX), and then there will be a clear period of 5 seconds. - violet_Dibbe commented on June 8th 19 at 17:11
June 8th 19 at 17:01
to use processes/threads. parent every 5 seconds spawns a new process. children's processes. work out your objectives.
June 8th 19 at 17:03
//Check whether the running processes of the controller script
if (getCurrentProcTasks(basename(__FILE__)) > 2) {
 exit("Already have a running copy of controller\n");
}

while (true) {
 // do somethings
 // here you can run the individual scripts that run for longer than 5 seconds via exec
sleep(5);
} 

getCurrentProcTasks function($php_file)
{
 $proc = [];
 $cmd = 'ps aux | grep "' . $php_file . '"';
 exec($cmd, $proc);
 $count = count($proc);
 return $count - 2;// 2 is the system of lines arising in the result of the grep command.
}


Here's a script you can run from the crown in times of 1-5 minutes, to monitor the implementation.

Find more questions by tags PHP