PHP: running scheduled jobs (cron jobs)

前端 未结 13 1821
自闭症患者
自闭症患者 2020-11-22 14:36

I have a site on my webhotel I would like to run some scheduled tasks on. What methods of achieving this would you recommend?

What I’ve thought out so far is having

13条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 15:09

    if you're wondering how to actually run your PHP script from cron, there are two options: Call the PHP interpreter directly (i.e., "php /foo/myscript.php"), or use lynx (lynx http://mywebsite.com/myscript.php). Which one you choose depends mostly on how your script needs its environment configured - the paths and file access permissions will be different depending on whether you call it through the shell or the web browser. I'd recommend using lynx.

    One side effect is that you get an e-mail every time it runs. To get around this, I make my cron PHP scripts output nothing (and it has to be nothing, not even whitespace) if they complete successfully, and an error message if they fail. I then call them using a small PHP script from cron. This way, I only get an e-mail if it fails. This is basically the same as the lynx method, except my shell script makes the HTTP request and not lynx.

    Call this script "docron" or something (remember to chmod +x), and then use the command in your crontab: "docron http://mydomain.com/myscript.php". It e-mails you the output of the page as an HTML e-mail, if the page returns something.

    #!/usr/bin/php
    Failed to open file: " . $_SERVER['argv'][1];
    }
    
    if ($h != '')
    {
            @mail("cron@mydomain.com", $_SERVER['argv']['1'], $h, "From: cron@mydomain.com\nMIME-Version: 1.0\nContent-type: text/html; charset=iso-8859-1");
    }
    
    ?>
    

提交回复
热议问题