通过CRON执行脚本

时间:2014-12-04 21:07:52

标签: php jquery cron cpanel

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

到目前为止,我已经尝试过,没有运气。任何帮助将不胜感激。

<!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>

2 个答案:

答案 0 :(得分:-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。

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

答案 1 :(得分:-5)

  1. 打开你的serverterminal,输入crontab -e
  2. 表示每分钟执行一次的cronjob:

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

  3. 保存文件

  4. 玩得开心:)