登录 |

PHP实现crontab

2013年05月6日 下午 34:39 | 作者:hemon

1. 准备一个标准crontab文件 ./crontab

# m h dom mon dow command
* * * * * date > /tmp/cron.date.run

2. crontab -e 将此cron.php脚本加入系统cron

* * * * * /usr/bin/php cron.php

3. cron.php 源码

// 从./crontab读取cron项,也可以从其他持久存储(mysql、redis)读取
$crontab = file('./crontab');
$now = $_SERVER['REQUEST_TIME'];

foreach ( $crontab as $cron ) {
	$slices = preg_split("/[\s]+/", $cron, 6);
	if( count($slices) !== 6 ) continue;

	$cmd       = array_pop($slices);
	$cron_time = implode(' ', $slices);
	$next_time = Crontab::parse($cron_time, $now);
	if ( $next_time !== $now ) continue;	

	$pid = pcntl_fork();
	if ($pid == -1) {
		die('could not fork');
	} else if ($pid) {
		// we are the parent
		pcntl_wait($status, WNOHANG); //Protect against Zombie children
	} else {
	     // we are the child
		`$cmd`;
		exit;
	}
}

/* https://github.com/jkonieczny/PHP-Crontab */
class Crontab {
   /**
	* Finds next execution time(stamp) parsin crontab syntax,
	* after given starting timestamp (or current time if ommited)
	*
	* @param string $_cron_string:
	*
	* 0 1 2 3 4
	* * * * * *
	* - - - - -
	* | | | | |
	* | | | | +----- day of week (0 - 6) (Sunday=0)
	* | | | +------- month (1 - 12)
	* | | +--------- day of month (1 - 31)
	* | +----------- hour (0 - 23)
	* +------------- min (0 - 59)
	* @param int $_after_timestamp timestamp [default=current timestamp]
	* @return int unix timestamp - next execution time will be greater
	* than given timestamp (defaults to the current timestamp)
	* @throws InvalidArgumentException
	*/
    public static function parse($_cron_string,$_after_timestamp=null)
    {
        if(!preg_match('/^((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+((\*(\/[0-9]+)?)|[0-9\-\,\/]+)$/i',trim($_cron_string))){
            throw new InvalidArgumentException("Invalid cron string: ".$_cron_string);
        }
        if($_after_timestamp && !is_numeric($_after_timestamp)){
            throw new InvalidArgumentException("\$_after_timestamp must be a valid unix timestamp ($_after_timestamp given)");
        }
        $cron = preg_split("/[\s]+/i",trim($_cron_string));
        $start = empty($_after_timestamp)?time():$_after_timestamp;

        $date = array( 'minutes' =>self::_parseCronNumbers($cron[0],0,59),
                            'hours' =>self::_parseCronNumbers($cron[1],0,23),
                            'dom' =>self::_parseCronNumbers($cron[2],1,31),
                            'month' =>self::_parseCronNumbers($cron[3],1,12),
                            'dow' =>self::_parseCronNumbers($cron[4],0,6),
                        );
        // limited to time()+366 - no need to check more than 1year ahead
        for($i=0;$i<=60*60*24*366;$i+=60){
            if( in_array(intval(date('j',$start+$i)),$date['dom']) &&
                in_array(intval(date('n',$start+$i)),$date['month']) &&
                in_array(intval(date('w',$start+$i)),$date['dow']) &&
                in_array(intval(date('G',$start+$i)),$date['hours']) &&
                in_array(intval(date('i',$start+$i)),$date['minutes'])

                ){
                    return $start+$i;
            }
        }
        return null;
    }

    /**
	* get a single cron style notation and parse it into numeric value
	*
	* @param string $s cron string element
	* @param int $min minimum possible value
	* @param int $max maximum possible value
	* @return int parsed number
	*/
    protected static function _parseCronNumbers($s,$min,$max)
    {
        $result = array();

        $v = explode(',',$s);
        foreach($v as $vv){
            $vvv = explode('/',$vv);
            $step = empty($vvv[1])?1:$vvv[1];
            $vvvv = explode('-',$vvv[0]);
            $_min = count($vvvv)==2?$vvvv[0]:($vvv[0]=='*'?$min:$vvv[0]);
            $_max = count($vvvv)==2?$vvvv[1]:($vvv[0]=='*'?$max:$vvv[0]);

            for($i=$_min;$i<=$_max;$i+=$step){
                $result[$i]=intval($i);
            }
        }
        ksort($result);
        return $result;
    }
}

Go练习(A Tour of Go)答案

2013年05月3日 上午 36:33 | 作者:hemon

Sqrt
用牛顿法实现开方函数。

package main

import (
    "fmt"
    "math"
)

func Sqrt(x float64) float64 {
    z := 1.0
    z_last := 1.0
    z_limit := 0.0000001
    for {
        z = (z+x/z)/2
        if math.Abs(z_last - z) < z_limit {
            break
        }
        z_last = z
    }
    return z
}

func main() {
    fmt.Println(Sqrt(5)==math.Sqrt(5))
}

Map
实现 `WordCount`。它应当返回一个含有 s 中每个 “词” 个数的 map。函数 wc.Test 针对这个函数执行一个测试用例,并打印成功或者失败。

package main

import (
    "code.google.com/p/go-tour/wc"
    "strings"
)

func WordCount(s string) map[string]int {
    m := make(map[string]int)
    words := strings.Fields(s)
    var word string

    for _,word = range(words) {
        m[word]++
    }

    return m
}

func main() {
    wc.Test(WordCount)
}

Fibonacci
实现一个 fibonacci 函数,返回一个函数(一个闭包)可以返回连续的斐波纳契数。

package main

import "fmt"

// 使用数组
func fibonacci() func() int {
    n := 0
    last := []int{1,1}
    return func () int {
        n++
        i := n%2
        if n > 2 {
            last[i] = last[0]+last[1]
        }

        return last[i]
    }
}
// 使用a/b两个变量
func fibonacci2() func() int {
    a, b := 0, 1
    return func () int {
        a = a+b
        b = a-b
        return a
    }
}

func main() {
    f := fibonacci()
    for i := 0; i < 10; i++ {
        fmt.Println(f())
    }
}

错误
从之前的练习中复制 Sqrt 函数,并修改使其返回 error 值。

package main

import (
    "fmt"
    "math"
)

type ErrNegativeSqrt float64

func (e ErrNegativeSqrt) Error() string {
	return fmt.Sprintf("cantnot Sqrt negative number: %v", float64(e))
}

func Sqrt(x float64) (float64, error) {
    if x < 0 {
        err := ErrNegativeSqrt(x)
        return 0, err
    }

    return math.Sqrt(x), nil
}

func main() {
    fmt.Println(Sqrt(2))
    fmt.Println(Sqrt(-2))
}

HTTP 处理
实现下面的类型,并在其上定义 ServeHTTP 方法。在 web 服务器中注册它们来处理指定的路径。

package main

import (
    "fmt"
    "net/http"
)

type Hello struct{}

type String string

type Struct struct {
    Greeting string
    Punct    string
    Who      string
}

func (s String) ServeHTTP(
    w http.ResponseWriter,
    r *http.Request) {
	response := fmt.Sprintf("%v", s)
    fmt.Fprint(w, response)
}

func (s Struct) ServeHTTP(
    w http.ResponseWriter,
    r *http.Request) {
	response := fmt.Sprintf("%v%v%v", s.Greeting,s.Punct,s.Who)
    fmt.Fprint(w, response)
}

func main() {
    http.Handle("/string", String("I'm a frayed knot."))
    http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"})
    http.ListenAndServe("localhost:4000", nil)
}

SSH端口转发

2013年04月17日 下午 33:25 | 作者:hemon

ssh-client和sshd-server之间支持双向数据转发

[*:*]<——redirect——[ssh-client]——–[sshd-server]——redirect——>[*:*]

  1. 本地转发 Local forwarded

    [src-host:port]—–>>—–[ssh-client:local-port]—–>>—–[sshd-server]—–>>—–[dst-host:port]

    在client打开一个监听端口,访问这个端口,经由sshd-server代理到指定地址(dst-host:port)

    ssh -L [bind_address:]local-port:dst-host:port [user@]hostname

  2. 远程转发 Remote forwarded

    [dst-host:port]—–<<—–[ssh-client]—–<<—–[sshd-server:remote-port]—–<<—–[src-host:port]

    在server打开一个监听端口,访问这个端口,经由ssh-client代理到指定地址(dst-host:port)

    ssh -R [bind_address:]remote-port:dst-host:port [user@]hostname

  3. 动态端口转发(本地)

    [src-host:port]——(socks4/5)—–>[ssh-client:local-port]—–>>—–[sshd-server]—–>>—–[dst-host*:port*]

    client作为socks代理服务器,打开一个监听端口,(使用SOCKS4/5协议)访问这个端口,经由server代理到任意地址(*:*)

    ssh -D [bind_address:]local-port [user@]hostname

默认只能监听本地环回地址(127.0.0.1),通过修改ssh_config/sshd_config,指定GatewayPorts = yes,可以监听所有地址(0.0.0.0)。

Loadrunner Generator for Linux

2012年12月25日 下午 58:47 | 作者:hemon

Ubuntu 12.04 安装步骤

#添加loadrunner帐号
useradd -s /bin/bash -m loadrunner
passwd loadrunner
su loaddrunner

#设置环境变量
vim ~/.bashrc
#Mercury LoadRunner8.1
export M_LROOT=/opt/Mercury
export LD_LIBRARY_PATH=${M_LROOT}/bin
export PATH=${M_LROOT}/bin:$PATH
export DISPLAY=localhost:0.0

#上传安装包,解压
unzip LoadRunner8.1.zip
cd LoadRunner8.1
#安装脚本赋予可执行权限
chmod a+x install.sh
#执行安装
sudo ./install.sh -console

#如果执行rsh,需要创建一个可信host文件
touch .rhosts
#验证安装
verify_generator

#放开文件描述符限制
ulimit -n 10240
#启动load-generator
m_daemon_setup start

mysql_connect localhost和127.0.0.1的区别?

2012年12月12日 下午 52:38 | 作者:hemon

connects.php

mysql_connect('127.0.0.1','root','zzzizzz1');
mysql_connect('localhost','root','zzzizzz1');

使用strace获取系统调用:

strace php connects.php 2>&1 | grep connect

#127.0.0.1 -> internet socket
connect(3, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)
#localhost -> unix domain socket
connect(7, {sa_family=AF_FILE, path="/var/run/mysqld/mysqld.sock"}, 110) = 0

Socket原本是为网络通讯设计的,但后来在Socket的框架上发展出一种IPC机制,就是UNIX Domain Socket。
虽然网络socket也可用于同一台主机的进程间通讯(通过loopback地址127.0.0.1),但是UNIX Domain Socket用于IPC更有效率:
不需要经过网络协议栈,不需要打包拆包、计算校验和、维护序号和应答等,只是将应用层数据从一个进程拷贝到另一个进程。
这是因为,IPC机制本质上是可靠的通讯,而网络协议是为不可靠的通讯设计的。
UNIX Domain Socket也提供面向流和面向数据包两种API接口,类似于TCP和UDP,但是面向消息(UDP)的UNIX Domain Socket也是可靠的,消息既不会丢失也不会顺序错乱。

参考:
http://lists.freebsd.org/pipermail/freebsd-performance/2005-February/001143.html

nginx limit_req限速设置

2012年11月26日 上午 13:17 | 作者:hemon

WIKI:

http://wiki.nginx.org/HttpLimitReqModule

漏桶原理(leaky bucket):

http://en.wikipedia.org/wiki/Leaky_bucket

实例:

#以用户二进制IP地址,定义三个漏桶,滴落速率1-3req/sec,桶空间1m,1M能保持大约16000个(IP)状态
limit_req_zone  $binary_remote_addr  zone=qps1:1m   rate=1r/s;
limit_req_zone  $binary_remote_addr  zone=qps2:1m   rate=2r/s;
limit_req_zone  $binary_remote_addr  zone=qps3:1m   rate=3r/s;

server {

#速率qps=1,峰值burst=5,延迟请求
#严格按照漏桶速率qps=1处理每秒请求
#在峰值burst=5以内的并发请求,会被挂起,延迟处理
#超出请求数限制则直接返回503
#客户端只要控制并发在峰值[burst]内,就不会触发limit_req_error_log
# 例1:发起一个并发请求=6,拒绝1个,处理1个,进入延迟队列4个:
#time    request    refuse    sucess    delay
#00:01        6        1        1            4
#00:02        0        0        1            3
#00:03        0        0        1            2
#00:04        0        0        1            1
#00:05        0        0        1            0
location /delay {
    limit_req   zone=qps1  burst=5;
}

#速率qps=1,峰值burst=5,不延迟请求
#加了nodelay之后,漏桶控制一段时长内的平均qps = 漏桶速率,允许瞬时的峰值qps > 漏桶qps
#所以峰值时的最高qps=(brust+qps-1)=5
#请求不会被delay,要么处理,要么直接返回503
#客户端需要控制qps每秒请求数,才不会触发limit_req_error_log
# 例2:每隔5秒发起一次达到峰值的并发请求,由于时间段内平均qps=1 所以仍然符合漏桶速率:
#time    request     refuse    sucess
#00:01         5         0          5
#00:05         5         0          5
#00:10         5         0          5
# 例3:连续每秒发起并发请求=5,由于时间段内平均qps>>1,超出的请求被拒绝:
#time    request     refuse     sucess
#00:01         5         0           5
#00:02         5         4           1
#00:03         5         4           1

location /nodelay {
    limit_req   zone=qps1  burst=5 nodelay;
}

}

closure闭包的好例子

2012年08月9日 下午 16:18 | 作者:hemon

var name = "The Window";
var object = {
    name : "My Object",
    getNameFunc : function(){
        var $this = this;
        console.log('In getNameFunc');
        console.log(this);
        console.log(this.name);
        return function(){
            console.log('In lamd');
            console.log($this);
            console.log($this.name);

            console.log(this);
            console.log(this.name);
        };
    }
};
var func = object.getNameFunc();
console.log(func);
func();

php:fopen文件打开模式对比

2012年07月26日 下午 08:20 | 作者:hemon

不枉此生

2012年07月23日 上午 25:57 | 作者:hemon

精神鸦片 0:13:07
不管你做什么,目的都是成功
精神鸦片 0:14:29
成功之道一直存在,只是你不知道
精神鸦片 0:15:14
你要做的,就是用你能够接受的成本,用各种办法,把那个正确的道路摸出来
精神鸦片 0:17:09
在寻找的路上,你的人格、意志得到锤炼,成为一个真正的人
此所谓:不枉此生

jQuery基于状态state,事件event编程

2012年07月14日 下午 32:44 | 作者:hemon

源文件:event




start

scan

weight

print


State = {
	pos    : 0,
	states : ['start','scan','weight','print'],
	set : function(state) {
		for (key in this.states) {
			if( this.states[key] == state ) {
				this.pos = key*1;
				break;
			}
		}

		$(".state").each(function(){
			var instate = $(this).attr('state').split(' ');
			if ( typeof(instate) == 'object' ) {
				var display = false;
				 for (key in instate)   {
					  if( instate[key] == state ) {
						  display = true;
						  break;
					  }
				 }

				 if ( display ) {
					 $(this).show();
				 } else {
					 $(this).hide();
				 }
			}
		})
	},
	next : function(loop) {
		var pos = this.pos + 1;
		if ( pos >= this.states.length ) {
			if ( loop ) {
				pos = 0;
			} else {
				return;
			}
		}
		this.set(this.states[pos]);
	},

	prev : function(loop) {
		var pos = this.pos - 1;
		if ( pos < 0 ) {
			if ( loop ) {
				pos = this.states.length-1;
			} else {
				return;
			}
		}
		this.set(this.states[pos]);
	}
};

Package = $({});

Package.bind('start print.end',function(e, data){
	State.set("start");
	// do start
	//Package.trigger('start.end',{order_id:123, order_status:'start'});
});

Package.bind('start.end',function(e, data){
	State.set("scan");
	// do scan
	//Package.trigger('scan.end',{order_id:123, order_status:'start'});
});

Package.bind('scan.end',function(e, data){
	State.set("weight");
	// do weight
	//Package.trigger('weight.end',{order_id:123, order_status:'start'});
});

Package.bind('weight.end',function(e, data){
	State.set("print");
	// do print
	//Package.trigger('print.end',{order_id:123, order_status:'start'});
});

Package.trigger("start");
 1 2 3 4 5 6 7 8 9 10 ...16 17 18 下一页
©hemono.com 赣ICP备07004153号