Is it possible to run a PHP script every hour or so without using cronjob?

I am sure I saw this in a php script once, although I cannot find the script. It was some script that automatically checks for updates for this script, and then replaces itself if there was an update.

I really don't need this, I just want my PHP script to run automatically every 30 minutes per hour, but I would like to do this without cronjobs, if possible.

Any suggestions? Or is it possible?

EDIT:. After reading a possible duplicate related to RC, I would like to clarify.

I would like to do this completely without using resources outside of the PHP script. AKA there are no external cronjobs that send a GET request. I would also like to do this without supporting the script constantly and sleep for 30 minutes

+4
source share
7 answers

If you have enough hits, it will work ...

Keep the latest update time somewhere (file, db, etc.). In a file that gets enough hits, add code that checks to see if the last update time was more than xx minutes ago. If that was then, run the script.

+3
source

You can use the PHP sleep function with a given time to run the code at this interval, or you can try several cron online services if you wish .

+3
source

You can use AJAX calls from real visitors to run scheduled tasks in the background (google for the β€œbad man cron”, there are a number of implementations) or use some external cron-like service (for example, cronjob on another machine). Theoretically, you can simply run a PHP script without a timeout and make it closed forever and debug requests at the appropriate time, but the only thing that can be achieved is to invent cron in a very inefficient and fragile way (if the script dies for some reason, it will never start on its own again, and cron will just call it again).

+1
source

Without supporting the script constantly, you will have to either use something hacky, which is not really guaranteed (when using regular user pages to start the side routine, to see if X has passed since the last run from the script, and if so, run it again) or use an external service like cron . There is no way for a regular PHP script to simply call itself magically.

0
source

If the host includes mysql 5.1 + db, then maybe timer triggers are available to invoke the script? I like these tasks related to impossible type, but you need more information about which playground and rules for a better answer.

0
source

In any case, you will need to set the runtime so that the script does not exceed it.

0
source

I found this:

 <?php // name of your file $myFile="time.db"; $time=file($myFile); if(time()-3600 > $time[0]){ // an hour has elapsed // do your thing. // write the new timestamp to file $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, time()); fclose($fh); } else{ // it hasn't been an hour yet, so do nothing } ?> 

in here

0
source

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


All Articles