How to run a PHP script in every 5-10 minutes with

2019-01-14 07:45发布

问题:

I want to run a script every 5 minutes on server. I have not any type of administrate privileges to do cron job etc.

I am not good php programmer so this question may look strange, but if you understand please tell me a solution.

回答1:

I use webcron.org wich is an affordable online service. Advantage for me is that I then have an overview of all scheduled tasks, on all servers.



回答2:

Sounds like your only solution would be "Poor Man's Cron".

What you do is create a script that you place on top of every page that only runs every 5 minutes. It won't be guaranteed to run every 5 minutes though. As it requires someone to visit the pages with the "poor man's cron" and if no one visits for over 5 minutes then it won't run until someone does.

Some example code:

<?php
 // load the last run time from a file, database, etc
 if(time() >= $last_run + (60 * 5)) { // 60 * 5 is 5 minutes
     // do your task here
     // save the last run time to a file, database, etc
 }


回答3:

Here's another 'hack'. Since you can't run cron on the machine where the script is, maybe you can run cron on another machine.

If you can...setup a cron job to run every 5 minutes...The job can be a simple PHP script that calls your other PHP script. You can use cURL to 'call' your script (if that script is being served up by a Web Server)



回答4:

See this and this.


(Edited to include content from the links.)

  • PHP Cron Daemon is driven by a database to schedule the execution of task (like cron). It can parse a crontab file and extract the job scheduling definitions into a MySQL database table. When it is time to run a scheduled job, it executes the job command in the crontab definition as PHP code as a separate process. The output of the job PHP code is also stored in a database table.


回答5:

That is how I do it

if(date('i', time())%5==0) {
// run this code
}


回答6:

Something like:

ini_set('max_execution_time', 'sometime'); 
while(1){
    //do something
    sleep(sometime);
}

Although I don't recommend doing this. Time to move to a server that meets your needs.



回答7:

I'm sure this tools can help you with your request:

SetCronJob - www.setcronjob.com

Cronless - cronless.com

EasyCron - www.easycron.com

Online Cron Jobs - www.onlinecronjobs.com

Corntab - www.corntab.com

getCron - www.getcron.com

However, it's not good idea to depend from another website.

Tanks to Google



回答8:

My solution if your server is Linux. Make bash script:

#bin/bash
while [ true ]
do
     //do whatever you want, use curl, lynx to your page...
     sleep 5m
done

Run it and if your server would not reboot or script would not killed you will be fine.

This one isn't good solution, but works.



回答9:

I would recommend this service, I used it today, and it seems to work well.

Cron-Job

and yes, it is Free!