监听服务端脚本

#!/bin/bash
day=`date -d “now” +%Y%m%d`
logdir=/home/users/sss/local/nginx/monitor/log/${day}
if [ ! -d $logdir ];then
mkdir -p $logdir
wait
fi;
netstat -tnpl |grep ‘:9999′
single=`echo $?`
if [ -n $single ] && [ $single -eq 1 ];then
now_time=`date -d “now” +%H:%M:%S`
echo ‘['${now_time}']try to restart…’ >> ${logdir}/work.log
/home/users/sss/local/php/bin/php-cgi -b 9999 -c /home/users/sss/local/php/etc/php.ini >> ${logdir}/work.log 2>>$logdir/wf.log 1>/dev/null &
exit
fi

发表在 bash | 留下评论

php中特殊的变量$argc/$argv

cli下经常会给脚本传递参数,php中用来接收参数的变量

function main($argc, $argv) {
printf(“params error, argc=%s, argv=%s”, $argc, print_r($argv, true));
}

main($argc,$argv);

==============================

output:

$ php test.php 2 `date +”%Y-%m-%d” -d “-1 days”`

params error, argc=3, argv=Array
(
[0] => test.php
[1] => 2
[2] => 2016-02-25
)

 

发表在 php | 留下评论

c10k问题

简单说就是网站同时有一万人在线,网站出现问题。

以下是参考文档

http://rango.swoole.com/archives/381

http://blog.acoak.com/article/c10k-problem-introduction

 

发表在 互联网 | 留下评论

Apache2.4 更新内容

配置不一样了

http://httpd.apache.org/docs/2.4/upgrading.html

发表在 apache | 留下评论

awk 打印倒数第n列字段

awk ' { print $(NF-1) }' file
发表在 awk | 留下评论

计数运算

$ export i=0
$ for F in `wallet_stat_db -N -e “select count(error_code) from t_custom_process_stat where act_date=’2016-01-17′ and app_code=’nuomi’ and process_id=1 and product_id=4 group by error_code; “`; do if [ $F -gt 1 ]; then i=$(($i+$F)); fi; done;
$ echo $i

发表在 bash | 留下评论

删除一些特殊字符命名的文件

有一次我由于误操作创建了一个有特殊符号的文件

drwxrwxr-x 6 v_haowenzhi v_haowenzhi 4096 Jan 15 16:45 svn
drwxr-xr-x 5 v_haowenzhi v_haowenzhi 4096 Jul 14 2015 svn_base
drwxrwxr-x 2 v_haowenzhi v_haowenzhi 4096 Mar 19 2015 temp
drwxrwxr-x 6 v_haowenzhi v_haowenzhi 4096 Dec 19 16:07 tmp
-rw-r–r– 1 v_haowenzhi v_haowenzhi 11153 Jan 18 11:26 tomer_id = ’3′) AND (wallet_branch_order_type = 1) AND (wallet_branch_order_value != ”) AND (wallet_branch
-rwxr-xr-x 1 v_haowenzhi v_haowenzhi 9059 Jun 9 2015 upload.py
drwxrwxr-x 3 v_haowenzhi v_haowenzhi 4096 Oct 10 13:15 work
drwxrwxr-x 5 v_haowenzhi v_haowenzhi 4096 Jul 28 15:26 www

 

这明显就是个sql字符串,这时可以用通配符删除

rm *wallet_branch_order_type*

发表在 bash | 留下评论

逐行读取文件中sql语句导入到mysql

cat ~/odp/app/etl/script/log/custom.log.20160117.sql |while read LINE; do wallet_stat_db -e “$LINE”; echo $LINE; wait;  done

 

文件格式大概如下:

INSERT wallet_stat_db.t_custom_process_stat SET Walk_track=’P1101_EU1103V1001_P1103′, Process_id=’1′, Page_step=’1′, Order_type=’1′, Product_id=’4′, Customer_id=’27′, loss_type=’2′, app_code=’bdread’, Event_code=’EU1130′, Event_step=’1′, Error_code=’15500′, Error_cate=’981′, Act_date=’2016-01-17′, Total_cnt=1
INSERT wallet_stat_db.t_custom_process_stat SET Walk_track=’P1101_EU1103V1001_P1103′, Process_id=’1′, Page_step=’1′, Order_type=’1′, Product_id=’4′, Customer_id=’27′, loss_type=’2′, app_code=’bdread’, Event_code=’EU1130′, Event_step=’1′, Error_code=’41004′, Error_cate=’624′, Act_date=’2016-01-17′, Total_cnt=1

发表在 bash | 留下评论

seq用法

 

Usage: seq [OPTION]… LAST
or: seq [OPTION]… FIRST LAST
or: seq [OPTION]… FIRST INCREMENT LAST
Print numbers from FIRST to LAST, in steps of INCREMENT.

-f, –format=FORMAT use printf style floating-point FORMAT (default: %g)
-s, –separator=STRING use STRING to separate numbers (default: \n)
-w, –equal-width equalize width by padding with leading zeroes
–help display this help and exit
–version output version information and exit

If FIRST or INCREMENT is omitted, it defaults to 1. That is, an
omitted INCREMENT defaults to 1 even when LAST is smaller than FIRST.
FIRST, INCREMENT, and LAST are interpreted as floating point values.
INCREMENT is usually positive if FIRST is smaller than LAST, and
INCREMENT is usually negative if FIRST is greater than LAST.
When given, the FORMAT argument must contain exactly one of
the printf-style, floating point output formats %e, %f, %g

 

 

demo1#seq -f “%3g+” 0 9

0+
1+
2+
3+
4+
5+
6+
7+
8+
9+

格式化并将前面填充三个空格

demo2#seq -f “%3g” -s, 0 9

0,  1,  2,  3,  4,  5,  6,  7,  8,  9

分割符,默认为\n

 

demo3#seq -s, -w 0 10

00,01,02,03,04,05,06,07,08,09,10

填充前置0和最大值一样的位宽

 

小提示:-w 和 -f不能一起使用,想想其实也是

发表在 bash | 留下评论

php多进程跑脚本

work.php主文件

#!/home/pay/odp/php/bin/php -c /home/pay/odp/php/etc/php.ini
<?php

/** 确保这个函数只能运行在SHELL中 */
if (substr(php_sapi_name(), 0, 3) !== ‘cli’) {
die(“This Programe can only be run in CLI mode”);
}
if (!function_exists(‘pcntl_fork’)) {
die(“pcntl_fork not existing”);
}

/** 关闭最大执行时间限制, 在CLI模式下, 这个语句其实不必要 */
set_time_limit(0);
/*
$pid = posix_getpid(); //取得主进程ID
$user = posix_getlogin(); //取得用户名
*/
$work_arr = array(
array(
‘file’ => ‘test.php’,
‘args’ => “arg1″,
),
array(
‘file’ => ‘test.php’,
‘args’ => “arg2″,
),
);
$process_arr = array();
foreach($work_arr AS $work){
$file = $work['file'];
$args = $work['args'];
if(!file_exists($file)){
continue;
}
$pid = pcntl_fork();
if($pis === -1){
echo “create process failed,file:{$file}\n”;
}elseif($pid == 0){//child process
$pid = posix_getpid();
$cmd = ‘/home/pay/odp/php/bin/php -c /home/pay/odp/php/etc/php.ini -q ‘.$file;
echo “* Process {$pid} was created, and Executed:\n\n”,$cmd.”\n\n”;
pcntl_exec(‘/home/pay/odp/php/bin/php’,array(‘-c /home/pay/odp/php/etc/php.ini’,$file),array(‘VAR_TEST’=>$args));
}else{//parent process
$process_arr[$file] = $pid;
}
}

//检查进程是否结束
$all_process_has_return = false;
$wait_options = WNOHANG;
while (!$all_process_has_return){
$all_process_has_return = true;//假设已经全部退出
foreach ($process_arr as $file => $pid){
$wait_ret = pcntl_waitpid($pid, $status, $wait_options);
if($wait_ret === 0){//只要还存在未退出的进程,已全部都退出就为false
$all_process_has_return = false;
}
}

}

?>

test.php为具体脚本

<?php
file_put_contents($_SERVER['VAR_TEST'].’_out.txt’,time());

?>

demo.php

参考资料:

http://ju.outofmemory.cn/entry/208177

发表在 php | 留下评论