1

i have a script which i am running from browser with meta refresh and it workd without any issue in browser but it will not work in cron so what i can do to run every second from cron? i know with sleep i can but i have to create several cron tab in cron job and every time i have to run the script

with sleep how can i run this script every 5 sec.

<meta http-equiv="refresh" content="5;url=test.php">
<?php
    $res = mysql_query("SELECT * FROM tableA where st='0' order by id asc LIMIT 1");
    $row = mysql_fetch_array($res);

    $link= $row['wl'];

    function getTitle($Url){
        $str = file_get_contents($Url);
        if(strlen($str)>0){
        preg_match("/\<\/td\><\/tr\><tr\><td colspan\=2\>(.*)\<\/td\>/",$str,$title);

            return $title[1];
        }
    }
    getTitle($link);
?>
Joel
  • 7,401
  • 4
  • 52
  • 58
user2761874
  • 21
  • 1
  • 1
  • 4

2 Answers2

9

Just add to your crontab

* * * * * for i in {0..59}; do curl http://your.domain.zone/page.html && sleep 1; done;

for added because cron could not run faster than once per minute.

sectus
  • 15,605
  • 5
  • 55
  • 97
  • how cron will know that when it has to be run – user2761874 Oct 26 '13 at 11:31
  • @user2761874, it knows because it's a scheduler. – sectus Oct 26 '13 at 11:34
  • getting error "-":21: bad command errors in crontab file, can't install. – user2761874 Oct 26 '13 at 11:35
  • 2
    This will cause a lag-behind: the execution of the script will take some time, then execution sleeps for 1 second, and so on; after 59 iterations you'll notice that total execution time might be longer than 1 minute, causing the script being executed twice within a near distance. – Marcel Korpel Oct 26 '13 at 12:04
  • @MarcelKorpel, you are right. It must to be a async calling with curl. – sectus Oct 26 '13 at 12:26
  • same error "-":21: bad command errors in crontab file, can't install. – user2761874 Oct 26 '13 at 12:33
  • @sectus if you will add same code as given by u in cron tab it is giving error.please check – user2761874 Oct 26 '13 at 13:45
  • @user2761874, it's works for me. But what is your real problem? – sectus Oct 28 '13 at 06:50
  • @sectus here is the screenshot of error. i am using exacatly same code with only link is different http://imgur.com/6qmna7t – user2761874 Oct 28 '13 at 17:39
  • @sectus this cron is creating problem for me as after every minute another instances are getting initiated and previous running instance is still running after the sleep of 1 sec so after 4-5 hours it is exceeding the server maximum connection limit and server will be down. can u please explain what this 0..59 does and why two dot u have used.if i increase this 59 to 1000000 and run that cron one in a year i think i can solve that issue ot i have to kill that running script every minute before starting another instance.please help – user2761874 Nov 17 '13 at 06:44
  • @MarcelKorpel this cron is creating problem for me as after every minute another instances are getting initiated and previous running instance is still running after the sleep of 1 sec so after 4-5 hours it is exceeding the server maximum connection limit and server will be down. can u please explain what this 0..59 does and why two dot u have used.if i increase this 59 to 1000000 and run that cron one in a year i think i can solve that issue ot i have to kill that running script every minute before starting another instance.please help – user2761874 Nov 17 '13 at 08:37
  • @user2761874, tell more about your real problem. – sectus Nov 17 '13 at 12:53
  • server is crossing maximum connection limit which is 250 every 4-5 hours and after that server is not responding then i have to restart the server. can u please explain your 0..59 .why you have used two dots and can i change 59 to some larger values .?? if this 59 is seconds then i cant change but every minute one new cron tab is starting however the previous one is still running with one second interval so i want to close/free previous connecton before starting new cron which is every minute – user2761874 Nov 17 '13 at 16:58
  • @user2761874, it's porblem of solution. What is your primordial problem? – sectus Nov 17 '13 at 23:37
  • @sectus my problem is how to close connection after every one minute for this script – user2761874 Nov 18 '13 at 17:45
  • Why the downvote? This definitely do the job for me, just make sure that your script wont fail if it have two or more instance running at the same time. – Hendyanto Nov 22 '14 at 04:02
1

Minimum call interval is 1 minute for cron

If you need more frequent calls, you have multiple choices:

  1. Create a daemon which is launched and then inside daemon you can put your own checks/sleep etc.
  2. You can listen to file system events to trigger processing of whatever tasks you need
  3. You can still use meta/js to "reload" page with combination of cron, but in that case you need to use headless browser such as phantomjs to handle your page and reloads properly. So you would be opening your "page" once a minute, and page would do 60/5=12 reloads itself using either js or meta tag.

clearly 3 is the worst. normally you would go either with option (1) or (2), depending on your system requirements.

jancha
  • 4,916
  • 1
  • 24
  • 39