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);
?>
1
  • 1
    You know the mysql_* functions are deprecated, don't you? Commented Oct 26, 2013 at 11:29

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

16 Comments

how cron will know that when it has to be run
@user2761874, it knows because it's a scheduler.
getting error "-":21: bad command errors in crontab file, can't install.
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.
@MarcelKorpel, you are right. It must to be a async calling with curl.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.