【问题标题】:Executing Scripts via CRON通过 CRON 执行脚本
【发布时间】:2014-12-05 05:20:10
【问题描述】:

当您在浏览器中手动访问页面时,我有一个 HTML 文件会触发两个 PHP 脚本。当我手动输入 URL 时,一切正常。我想知道如何将这个脚本作为 cron 运行,以便它在给定的时间间隔内不断触发?

到目前为止,我已经尝试过 wget 和 curl,但没有成功。任何帮助将不胜感激。

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="js/jquery.shoutcast.min.js"></script>
    <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Kaushan Script">
    <!-- Current Song Played -->
    <script>
        // Get current song playing and load it into an element with an ID of songtitle
        $.SHOUTcast({
            host : 'live.soundcheck.xyz',
            port : 8000,
            interval : 5000,
        }).stats(function(){
            $('#songtitle').text(this.get('songtitle'));

            $(document).ready(function() {
              //  console.log("Document Ready!");
                var content = $('#songtitle').text();
                var music = $('#songtitle').text();
                var song = $('#songtitle').text();
                var title = $('#songtitle').text();
            //    console.log(content);

                // $.post("nowplaying.php", //Required URL of the page on server
                $.post("ignore.php", //Required URL of the page on server
                    {   // Data Sending With Request To Server
                        name:content,
                    },
                    function(response){  // Required Callback Function
                        //    alert("Response: " + response);  //  "response"  receives - whatever written in echo of above PHP script.

                });
                $.post("new/update.php", //Required URL of the page on server
                    {   // Data Sending With Request To Server
                        track:music,
                    },
                    function(response){  // Required Callback Function
                       // alert("Response: " + response);  //  "response"  receives - whatever written in echo of above PHP script.
                     //  console.log(response);
                });
                /*$.post("current.php", //Required URL of the page on server
                    {   // Data Sending With Request To Server
                        current:song,
                    },
                    function(response){  // Required Callback Function
                       // alert("Response: " + response);  //  "response"  receives - whatever written in echo of above PHP script.
                       console.log(response);
                });*/

            });

        });
    </script> 

    <style type="text/css">
        #nowplaying{
            border: solid;
            border-color: black;
            width: 750px;
            height: auto;
            padding: 5px 5px 5px 5px;
            float: left;
        }
        #label{
            font-weight: 500;
            font-family: Kaushan Script;
            font-size: xx-large;
        }
        #songtitle{
            font-weight: 300;
            font-family: fantasy;
            font-size: medium;
        }
        ol.labels-list {
            list-style-type: none;
            margin: 0;
            margin-left: 1em;
            padding: 0;
            counter-reset: li-counter;
        }
        ol.labels-list > li{
            position: relative;
            margin-bottom: 1.5em;
            padding: 1.5em;
            background: #45484d; /* Old browsers */
            background: -moz-linear-gradient(left,  #45484d 0%, #000000 100%); /* FF3.6+ */
            background: -webkit-gradient(linear, left top, right top, color-stop(0%,#45484d), color-stop(100%,#000000)); /* Chrome,Safari4+ */
            background: -webkit-linear-gradient(left,  #45484d 0%,#000000 100%); /* Chrome10+,Safari5.1+ */
            background: -o-linear-gradient(left,  #45484d 0%,#000000 100%); /* Opera 11.10+ */
            background: -ms-linear-gradient(left,  #45484d 0%,#000000 100%); /* IE10+ */
            background: linear-gradient(to right,  #45484d 0%,#000000 100%); /* W3C */


        }
        ol.labels-list > li:before {
            position: absolute;
            top: -0.3em;
            left: -0.5em;
            width: 1.8em;
            height: 1.2em;
            font-size: 2em;
            line-height: 1.2;
            font-weight: bold;
            text-align: center;
            color: #464646;
            background-color: #d0d0d0;

            transform: rotate(-20deg);
            -ms-transform: rotate(-20deg);
            -webkit-transform: rotate(-20deg);
            z-index: 99;
            overflow: hidden;
            content: counter(li-counter);
            counter-increment: li-counter;
        }
        .lastplayed{
            font-weight: 300;
            font-family: Kaushan Script;
            font-size: medium;
            color: white;
        }
        #imgholder{
            width: 50%;
            margin: 0 auto;
        }
        .text-glow{
            color: #000000;
            font-family: Arial;
            font-size: 4em;
            margin: 0 auto;
            text-align: center;
            width: 1000px;
            padding-top:75px;
        }
        #nowplaying{
            text-align: center;
        }

        .text-glow:hover,
        .text-glow:focus
        .text-glow:active{
            -webkit-stroke-width: 5.3px;
            -webkit-stroke-color: #000000;
            -webkit-fill-color: #FFFFFF;
            text-shadow: 1px 0px 20px #F90;
            -webkit-transition: width 0.3s; /*Safari &amp; Chrome*/
            transition: width 0.3s;
            -moz-transition: width 0.3s; /* Firefox 4 */
            -o-transition: width 0.3s; /* Opera */

        }

        .text-glow a{

            -webkit-transition: all 0.3s ease-in; /*Safari &amp; Chrome*/
            transition: all 0.3s ease-in;
            -moz-transition: all 0.3s ease-in; /* Firefox 4 */
            -o-transition: all 0.3s ease-in; /* Opera */

            text-decoration:none;
            color:#000000;
        }
        #songtitle{
            font-size: x-large;
        }
    </style>                                        
</head>
<body>

    <div id="reload"><span id="songtitle" class="now"></span></div>
    <div id="php"><?php echo date('l, F jS, Y'); ?></div>
    <div id="php2"><?php echo $_POST["name"]; ?></div>

</body>

【问题讨论】:

  • 它的js,你需要一个浏览器才能运行它。仍然可以通过 cron 完成,但它很痛苦 - 你可以将它转换为纯服务器端吗?
  • 您可能想查看Codeception 之类的内容并为其编写测试。 Codeception 使用 phpBrowser 并且可以处理诸如 javascript 之类的东西。然后就像@Dagon 说的那样,只需安排一个 cron 来启动测试,无论你喜欢什么频率。
  • 这真的很不现实,但是你可以用支持现代js的基于文本的浏览器打开页面(通过cron)。 elinks 是我所知道的唯一一个,如果用 spidermonkey 编译的话

标签: php jquery cron cpanel


【解决方案1】:

在您的情况下,如果您在 Linux 服务器上,则应将整个 html/jquery 内容放入 .php 文件中,并将其保存在可执行文件和 Web 可访问并发出的位置:

sudo crontab -e //this will open the crontab in whatever editor

以这种格式插入 cronjob

*/10 *  *  *  *     curl 'http://host/php-script-location/php-script.php' 
//feel free to pass in parameters. 

确保调整 cron 的时间,这个 (*/10 * * * *) 需要调整,以便为 cronjob 设置正确的运行时间

使用cURL,不要使用wget,cURL 模拟浏览器一样的动作

然后发出crontab -l 以确保您的cronjob 在那里。它会打印出行 显示你的 cronjob。

做一些测试,你应该没问题。

【讨论】:

  • curl 也不会执行js
  • 试试这个 "/usr/bin/firefox host/html-location/html-file.html 或试试 NodeJS。确保你的系统上安装了 firefox。有一种方法可以编写一个使用 Javascript 的 cURL 脚本,但可能对你来说很耗时。
  • 那么这台机器上最终会运行多少个 firefox 副本?
  • 很多。如果您在完成后担心脚本问题中的 kill -KILL firefox-pid 。通过发出 ps -e | 找到 firefox ps grep“火狐”。根据您的服务器,这可能会成为超时问题。
【解决方案2】:
  1. 打开你的服务器终端,输入 crontab -e
  2. 对于每分钟执行的 cronjob 类型:

    * * * * * php /path/to/your/File

  3. 保存文件

  4. 玩得开心:)

【讨论】:

    猜你喜欢
    • 2012-11-14
    • 2023-03-06
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 2012-03-12
    • 2014-09-06
    相关资源
    最近更新 更多