【问题标题】:Cron Job to rename web pages用于重命名网页的 Cron 作业
【发布时间】:2015-01-11 10:20:06
【问题描述】:

首先,让我说我对 Cron Jobs 或 PHP 文件一无所知,所以如果您愿意回复,请记住我的新手身份 - (用小词,慢慢说!)

我是我们组织网站的网站管理员,我正在尝试做一些简单但耗时的事情:获取我们网站上的主页,并在感恩节和圣诞节假期期间自动更新它。我已经创建了我需要的特定网页,并将它们上传到 GoDaddy 服务器,但是我真的没有时间进入并随着日子的临近手动重命名页面;有人告诉我,Cron Job 只是用来自动保存现有页面并重命名新页面的东西。

这就是我想要做的: 1) 位于根目录中的文件 Index.htm 被保存或重命名,以便我可以在假期结束后返回它。 2) 现在名为 /Holiday Pages/happy_thanksgiving.htm 的文件于 11 月 25 日移至根目录并重命名为 index.htm 与 12 月 24 日名为 merry_christmas.htm 的文件相同。 3) GoDaddy 有一个 Cron Job 控制面板,它允许我在特定日期的特定时间运行特定脚本,所以我认为不需要将日期代码嵌入到脚本本身中 - 但我没有一个线索在这个脚本中放什么 - 通过与 GoDaddy 的人们交谈,他们建议使用 PHP 脚本。 4)在这个 PHP 脚本中,我需要编写哪些命令(请提供具体示例 - 示例脚本会很棒,非常感谢! 5) 这个脚本的扩展名需要是什么? .TXT 还是 .PHP ??

提前致谢!再一次,请记住我在我的头上 - 请原谅我的无知!

----------------------------------- --------更新 111/15/14 ------------------------ -------------------- 这是我迄今为止尝试过的,使用了您的一些建议 - 数字 1) 2) 等是试用脚本编号,然后由 GoDaddy 的 Cron 作业管理器调用。

1) --------------------------------------------- -

<?php
$target = "/holiday_pages/happy_thanksgiving.html"; 
$newName = "/holiday_pages/index.htm";
$renameResult = rename($target, $newName);
// Evaluate the value returned from the function if needed
if ($renameResult == true) {
echo $target . " is now named " . $newName;
} else {
 echo "Could not rename that file";
}
?>

2) --------------------------------------------- ---

rename('/holiday_pages/happy_thanksgiving.html', '/holiday_pages/index.htm');

3) --------------------------------------------- ---

rename("/holiday_pages/happy_thanksgiving.html", "/home/user/password/holiday_pages/index.htm");

?> 

4) --------------------------------------------- ---

<?php

$date = new DateTime();
$date -> format('Y.m.d');

if ($date == '2014.11.15') {
copy('./HTML/holiday_pages/index.htm','index.htm.bak');
copy('./HTML/holiday_pages/happy_thanksgiving.html','index.htm'); // you need to make sure      if this (../Holiday Pages/) is the right path to your file!

}
else if ($date == '2014.11.26') {
copy('index.htm.bak','index.htm');

}

5) --------------------------------------------- -----

<?php

$date = new DateTime();
$date -> format('Y.m.d');

if ($date == '2014.11.15') {
copy('../HTML/holiday_pages/index.htm','index.htm.bak');
copy('../HTML/holiday_pages/happy_thanksgiving.html','index.htm'); // you need to make sure if this (../Holiday Pages/) is the right path to your file!

}
else if ($date == '2014.11.26') {
copy('index.htm.bak','index.htm');

}

6) --------------------------------------------- -----

<?php

$date = new DateTime();
$date -> format('Y.m.d');

if ($date == '2014.11.15') {
copy('/HTML/holiday_pages/index.htm','index.htm.bak');
copy('/HTML/holiday_pages/happy_thanksgiving.html','index.htm'); // you need to make sure if     this (../Holiday Pages/) is the right path to your file!

}
else if ($date == '2014.11.26') {
copy('index.htm.bak','index.htm');

}

7) --------------------------------------------- ------

<?php

$date = new DateTime();
$date -> format('Y.m.d');

if ($date == '2014.11.15') {
copy('./holiday_pages/index.htm','index.htm.bak');
copy('./holiday_pages/happy_thanksgiving.html','index.htm'); // you need to make sure if this (../Holiday Pages/) is the right path to your file!

}
else if ($date == '2014.11.26') {
copy('index.htm.bak','index.htm');

【问题讨论】:

  • 我已经尝试了几种脚本变体,但到目前为止都没有运气。奇怪的是,我没有收到任何从 Cron 管理器发送到我的电子邮件的错误消息 - 几乎就像没有任何东西在运行,但 Cron 管理器说脚本已启用。我已经检查以确保每个 PHP 文件都有 777 权限,甚至尝试了嵌入我的用户名和密码的变体......仍然无法正常工作。仅供参考,我每个月只有大约 1 小时用于网站维护,所以让这种自动化工作绝对是我的首要任务!

标签: php cron jobs


【解决方案1】:

更好的解决方案是根据日期动态创建文件和代码,示例代码如下:

$date = date("m-d-y", time());  // current date

$holidays = array('12-25-14' => 'christmas-page.php', '11-27-14' => 'thanksgiving-day-page.php'); // create all holidays here

$page = isset($holidays[$date]) ? $holidays[$date] : 'default-index.php';

include($page);

简单的逻辑,可以正常工作

【讨论】:

    【解决方案2】:

    只需创建一个文件并为其命名,例如 holiday_copy.php。这是脚本本身:

    <?php
    
    $date = new DateTime();
    $date -> format('Y.m.d');
    
    if ($date == '2014.11.25') {
        copy('index.htm','index.htm.bak');
        copy('../Holiday Pages/happy_thanksgiving.htm','index.htm'); // you need to make sure if this (../Holiday Pages/) is the right path to your file!
    
    }
    else if ($date == '2014.11.26') {
        copy('index.htm.bak','index.htm');
    }
    else if ($date == '2014.12.24') {
        copy('index.htm','index.htm.bak');
        copy('../Holiday Pages/merry_christmas.htm','index.htm'); // again, you need to make sure if this (../Holiday Pages/) is the right path to your file!
    }
    else if ($date == '2014.12.27') {
        copy('index.htm.bak','index.htm');
    }
    

    ?>

    你必须在想要的日子里运行这个脚本,最好是在

    0:01 on eg 2014-12-24
    

    您需要将此文件放在您的 index.htm 所在的根目录中。如果您不想这样做,请在此脚本中添加所有复制文件的适当路径 - 例如这里:

    copy('index.htm','index.htm.bak');
    

    确保拥有访问所有目录和文件的权限!记得在假期后的几天里调用它,以便找回旧的 index.htm。

    【讨论】:

    • 谢谢大家 - 我会尝试这些解决方案。今天早上我已经为此工作了大约 3 个小时,但到目前为止,我尝试过的任何方法都没有奏效。我将 PHP 文件的文件权限设置为 777 - GoDaddy Cron Job CPanel 似乎负责其中的调度部分。我确实打电话给 GoDaddy,我与之交谈的人都说它应该可以工作 - 我假设他检查以确保服务器设置正确。我们使用的是 Linux 服务器,我不认为它是“共享的”。
    【解决方案3】:

    这不是 PHP 解决方案。这是 SSH/命令行解决方案,也不是 Cron 作业。大约需要 2-5 分钟。

    假设您使用的是 Linux 托管计划,并且为您的帐户启用了 SSH(远程登录);如果没有,您可以要求/致电 GoDaddy 为您的帐户启用此功能。

    进入命令行后,请执行以下操作。

    安全第一,所以备份我们要移动或弄乱的所有文件:

    1. cd PUBLIC_ROOT_DIR
    2. cp "index.htm" "index.htm.bak"
    3. cp "Holiday Pages/happy_thanksgiving.htm" "Holiday Pages/happy_thanksgiving.htm.bak"
    4. cp "Holiday Pages/merry_christmas.htm" "Holiday Pages/merry_christmas.htm.bak"

    感恩节前:

    1. cd PUBLIC_ROOT_DIR
    2. mv "index.htm" "index.htm.off"
    3. mv "Holiday Pages/happy_thanksgiving.htm" "index.htm"

    感恩节过后:

    1. cd PUBLIC_ROOT_DIR
    2. mv "index.htm" "happy_thanksgiving.htm.off"
    3. mv "index.htm.off" "index.htm"

    圣诞节前:

    1. cd PUBLIC_ROOT_DIR
    2. mv "index.htm" "index.htm.off"
    3. mv "Holiday Pages/merry_christmas.htm" "index.htm"

    圣诞节过后:

    1. cd PUBLIC_ROOT_DIR
    2. mv "index.htm" "merry_christmas.htm.off"
    3. mv "index.htm.off" "index.htm"

    【讨论】:

    • 我承认我不是 PHP 专家。一般来说,可执行脚本应该有755权限。
    【解决方案4】:

    正如你们中的许多人所提到的,CRON JOB 不是一个好的选择。而是一个重定向脚本,它根据服务器日期将 Web 用户带到正确的页面。我能够从一个以编程为生的朋友那里得到一些很大的帮助——尽管这篇文章已经有一年多了,但我想我会分享最终结果和工作脚本,以防其他人可能有同样的问题。请注意,此脚本安装在 Web 服务器的根目录中并命名为“index.html”。还会创建一个用于非节假日使用的网页,称为“main.html”,如果不满足条件列表,脚本将使用此页面。

    这个解决方案非常适合我们,我非常感谢所有的回复!

     <!DOCTYPE html>
    <!--
    -->
    <html>
    <head>
        <title></title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
    
        <script type="text/javascript">
            var today = new Date();
    
            var newyear1 = new Date("2016-12-30");
            var newyear2 = new Date("2016-01-05");
    
        var valentines1 = new Date("2016-02-14");
            var valentines2 = new Date("2016-02-16");
    
        var spring_forward1 = new Date("2016-03-07");
            var spring_forward2 = new Date("2016-03-16");
    
            var easter1 = new Date("2016-03-20");
            var easter2 = new Date("2016-03-29");
    
        var easter3 = new Date("2016-03-28");
            var easter4 = new Date("2016-03-30");
    
        var mothers_day1 = new Date("2016-05-07");
            var mothers_day2 = new Date("2016-05-11");
    
        var memorial_day1 = new Date("2016-05-27");
            var memorial_day2 = new Date("2016-06-02");
    
        var fathers_day1 = new Date("2016-06-18");
            var fathers_day2 = new Date("2016-06-21");
    
        var july_4th1 = new Date("2016-07-04");
            var july_4th2 = new Date("2016-07-06");
    
        var laborday1 = new Date("2016-09-04");
            var laborday2 = new Date("2016-09-09");
    
        var election1 = new Date("2016-11-07");
            var election2 = new Date("2016-11-10");
    
        var thanksgiving1 = new Date("2016-11-20");
            var thanksgiving2 = new Date("2016-11-30");
    
        var xmas1 = new Date("2016-12-10");
            var xmas2 = new Date("2016-12-30");
    
            // default index page. if no date ranges match, this page will     be     used.
            var pageName = "main.html";
    
    
            // new_year 2016
            if(today >= newyear1 && today <= newyear2)
            {
                pageName = "/holiday_pages/happy_new_year.html";
            }
    
            // Valentines 2016
            if(today >= valentines1 && today <= valentines2)
            {
                pageName = "/holiday_pages/valentines.html";
            }
    
            // time_change_forward 2016
            if(today >= spring_forward1 && today <= spring_forward2)
            {
                pageName = "/holiday_pages/spring_forward.html";
            }
    
            // Easter General 2016
            if(today >= easter1 && today <= easter2)
            {
                pageName = "/holiday_pages/easter_1.html";
            }
    
            // Easter Resurrection 2016
            if(today >= easter3 && today <= easter4)
            {
                pageName = "/holiday_pages/easter_2.html";
            }
    
            // Mothers Day 2016
            if(today >= mothers_day1 && today <= mothers_day2)
            {
                pageName = "/holiday_pages/mothers_day.html";
            }
    
            // Memorial Day 2016
            if(today >= memorial_day1 && today <= memorial_day2)
            {
                pageName = "/holiday_pages/memorial_day.html";
            }
    
             // Fathers Day 2016
            if(today >= fathers_day1 && today <= fathers_day2)
            {
                pageName = "/holiday_pages/fathers_day.html";
            }
    
            // July 4th 2016
            if(today >= easter3 && today <= july_4th)
            {
                pageName = "/holiday_pages/july_4th.html";
            }
    
            // labor_day 2016
            if(today >= laborday1 && today <= laborday2)
            {
                pageName = "/holiday_pages/labor_day.html";
            }
    
            // Election Day 2016
            if(today >= election1 && today <= election2)
            {
                pageName = "/holiday_pages/election_day.html";
            }
    
            // Thanksgiving 2016
            if(today >= thanksgiving1 && today <= thanksgiving2)
            {
                pageName = "/holiday_pages/happy_thanksgiving.html";
            }
    
            // Christmas 2016
            if(today >= xmas1 && today <= xmas2)
            {
                pageName = "/holiday_pages/merry_christmas.html";
            }
    
    
    
            // redirect to pageName
            document.location.href = pageName;
    
        </script>
     </body>
     </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-12
      • 2019-02-20
      • 1970-01-01
      • 2013-02-01
      • 2020-06-10
      相关资源
      最近更新 更多