Discuz!官方免费开源建站系统

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

[素材] UChome计划任务辅助程序,在服务器上运行可减少压力!

[复制链接]
liuzhly 发表于 2009-12-2 16:09:47 | 显示全部楼层 |阅读模式
UCHome插件
语言编码: GBK简体 
插件名称: UChome计划任务辅助程序
插件作者: 本人
适用版本: UCHome 2.0
本帖最后由 liuzhly 于 2010-01-05 12:42 编辑

function_cron.php 修改如下代码:

//执行计划任务
function runcron($cronid = 0,$auto=0) {
        global $_SGLOBAL, $_SCONFIG, $_SBLOCK, $_TPL, $_SCOOKIE, $_SN, $space;
        include_once(S_ROOT.'./auto_cron.lock');
        if($auto == 0 && $cronid == 0){ //代表后台运行,时间是自动运行时间差小于600
                if(defined('AUTO_CRON')) {
                        if($_SGLOBAL[timestamp] - AUTO_CRON < 600){return NULL;}
                }
        }

        $where = $cronid ? "cronid='$cronid'" : "available>'0' AND nextrun<='$_SGLOBAL[timestamp]'";
        $query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('cron')." WHERE $where ORDER BY nextrun LIMIT 1");//只运行一个
        if($cron = $_SGLOBAL['db']->fetch_array($query)) {

                $lockfile = S_ROOT.'./data/runcron_'.$cron['cronid'].'.lock';
                $cronfile = S_ROOT.'./source/cron/'.$cron['filename'];

                if(is_writable($lockfile)) {
                        $locktime =  filemtime($lockfile);
                        if($locktime > $_SGLOBAL['timestamp'] - 600) {//10分钟
                                return NULL;
                        }
                } else {
                        @touch($lockfile);
                }

                @set_time_limit(1000);
                @ignore_user_abort(TRUE);

                cronnextrun($cron);

                if(!@include $cronfile) {
                        runlog('CRON', $cron['name'].' : Cron script('.$cron['filename'].') not found or syntax error', 0);
                }

                @unlink($lockfile);
        }
       
        //更新config
        cron_config();
}


主要思想:如果后台程序运行中,那么用户就不用打开页面时候执行计划任务,如果后台不运行,那么用户打开页面就会执行计划任务 ,判断时间差是10分钟

然后在UCHOME目录下建立两个文件
auto_cron.php
auto_cron.lock  //手动建立即可,可为空

auto_cron.php内容:

<?php
header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header ( "Last-Modified: " . gmdate ( "D, d M Y H:i:s" ) . " GMT" );
header ( "Cache-Control: no-store, no-cache, must-revalidate" );
header ( "Cache-Control: post-check=0, pre-check=0", false );
header ( "Pragma: no-cache" );
/*
        [UCenter Home] (C) 2007-2008 Comsenz Inc.
        $Id: admincp.php 13141 2009-08-13 01:48:28Z xupeng $
*/

include_once('./common.php');
include_once(S_ROOT.'./source/function_cron.php');
$atimestamp = $_SGLOBAL[timestamp];
runcron(0,1);
$autofile = S_ROOT.'auto_cron.lock';
$autotext = "<?php\r\ndefine('AUTO_CRON', {$atimestamp});\r\n\r\n?>";
if(swritefile($autofile, $autotext)){
        $atimestamp = date('Y-m-d H:i:s', $atimestamp);
        $mess_ = "[{$atimestamp}] OK,本次运行完毕";
}else{
        $mess_ = "文件写入错误";
}
?>


然后输入 在服务器上使用程序刷新这个文件吧...

附件提供替换文件 !!


主要目的:减轻客户端压力,减少计划任务出错机会!!!

注意,蓝色代码今天刚改进的,加上后可手动运行了,原来忘记判断了,12月3日最新修改为蓝色字体,请自己加上吧,不好意思!附件我就不更新了,下载完毕自己修改一下就OK

=============2010年01月05日最新修改============

2003 IIS 可用!
用此红色代码存成VBS替代 start.bat 或者 HTML ,注意在 auto_cron.php 最顶上加代码如下蓝色代码用户VBS判断网页状态!
<TITLE>New Document</TITLE>

红色代码开始:


On Error Resume Next
Set Wshell=WScript.CreateObject("WScript.Shell")
Set ie=WScript.CreateObject("InternetExplorer.Application")
Dim rcount
rcount=0
ie.visible=false
WScript.Sleep 60000  '暂停60000毫秒
Do While True
        ie.navigate "http://uc.yt.sd.sgcc.com.cn/auto_cron.php" '用IE打开自动运行网页
        wscript.sleep 133000 '休眠133秒
        If rcount=10 Then
                Wshell.run "shutdown -r -t 10 -c ""Restart Computer""",0 '发现10次错误重新启动计算机
                Exit Do
        End If
        If ie.document.title <> "New Document" Then '如果网页无法访问,重启iis
                rcount=rcount+1
                Wshell.Run "%COMSPEC% /c IISRESET.EXE",,1
                WScript.Sleep 300000  '暂停300秒,IIS重新启动
        End If
Loop


将代码存成start.vbs 用于监控网站,由于经常使用农场导致IIS假死,可用此方法自动重启!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

评分

3

查看全部评分

 楼主| liuzhly 发表于 2009-12-2 16:22:17 | 显示全部楼层
本帖最后由 liuzhly 于 2009-12-30 10:25 编辑

另外,已修正 计划任务 无缘无故 自动停止的BUG...

提供方法找到  function_cron.php  然后找到如下函数:


function cronnextrun($cron) {
        global $_SGLOBAL, $_SCONFIG;

        if(empty($cron)) return FALSE;

        list($yearnow, $monthnow, $daynow, $weekdaynow, $hournow, $minutenow) = explode('-', sgmdate('Y-m-d-w-H-i', $_SGLOBAL['timestamp']));
       
        $cron['minute'] = explode("\t", $cron['minute']);
       
        if($cron['weekday'] == -1) {
                if($cron['day'] == -1) {
                        $firstday = $daynow;
                        $secondday = $daynow + 1;
                } else {
                        $firstday = $cron['day'];
                        $secondday = $cron['day'] + sgmdate('t', $_SGLOBAL['timestamp']);
                }
        } else {
                $firstday = $daynow + ($cron['weekday'] - $weekdaynow);
                $secondday = $firstday + 7;
        }

        if($firstday < $daynow) {
                $firstday = $secondday;
        }

        if($firstday == $daynow) {
                $todaytime = crontodaynextrun($cron);
                if($todaytime['hour'] == -1 && $todaytime['minute'] == -1) {
                        $cron['day'] = $secondday;
                        $nexttime = crontodaynextrun($cron, 0, -1);
                        $cron['hour'] = $nexttime['hour'];
                        $cron['minute'] = $nexttime['minute'];
                } else {
                        $cron['day'] = $firstday;
                        $cron['hour'] = $todaytime['hour'];
                        $cron['minute'] = $todaytime['minute'];
                }
        } else {
                $cron['day'] = $firstday;
                $nexttime = crontodaynextrun($cron, 0, -1);
                $cron['hour'] = $nexttime['hour'];
                $cron['minute'] = $nexttime['minute'];
        }

        //更新下次运行时间
        $nextrun = @gmmktime($cron['hour'], $cron['minute'], 0, $monthnow, $cron['day'], $yearnow) - $_SCONFIG['timeoffset'] * 3600;
        $setarr = array(
                'lastrun' => $_SGLOBAL['timestamp'],
                'nextrun' => $nextrun
        );
        //if($nextrun <= $_SGLOBAL['timestamp']) {
        //        $setarr['available'] = 0;
        //}
       
        updatetable('cron', $setarr, array('cronid'=>$cron['cronid']));
        return TRUE;
}


看到红色的了么??前面加//就OK!!
回复

使用道具 举报

526144990 发表于 2009-12-2 17:11:15 | 显示全部楼层
好冷的帖子,火来了。
回复

使用道具 举报

zhuhaiyemen 发表于 2009-12-2 17:15:25 | 显示全部楼层
不错支持你一下!呵呵
回复

使用道具 举报

foolant 发表于 2009-12-2 17:19:10 | 显示全部楼层
又见高手。。。。
回复

使用道具 举报

diomen 发表于 2009-12-2 22:22:51 | 显示全部楼层
很不错,支持一下
回复

使用道具 举报

coldhair 发表于 2009-12-2 22:25:21 | 显示全部楼层
不错啊,挺好
回复

使用道具 举报

ゾ芯︵亂ジ 发表于 2009-12-2 22:59:09 | 显示全部楼层
不错啊,挺好
回复

使用道具 举报

kingtung 发表于 2009-12-3 00:51:53 | 显示全部楼层
回复

使用道具 举报

hi.boyfriend 发表于 2009-12-3 01:02:43 | 显示全部楼层
不太明白..-.-
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|Discuz! 官方站 ( 皖ICP备16010102号 )star

GMT+8, 2024-9-28 09:29 , Processed in 0.169355 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表