守护进程
-
脚本
#!/bin/bash _opt=$1 main () { case "${_opt}" in start) start ;; stop) stop ;; restart) restart ;; *) usage exit 1 esac } start() { printf "Starting Worker Queue...\n" ARRAY_IP=(10.101.0.4 10.101.0.6 10.101.0.26) # 机器数 j=0; for ip in ${ARRAY_IP[@]};do ssh root@$ip "nohup sh /opt/webroot/iyuansong/current/bin/fork_push.sh /amqp/amqp_sync_plus/consume/${j} 15 >/dev/null 2>&1 &" ((j++)); done } stop () { printf "Stoping Worker Queue...\n" ARRAY_IP=(10.101.0.4 10.101.0.6 10.101.0.26) for ip in ${ARRAY_IP[@]};do ssh root@$ip "ps aux|grep '/amqp/amqp_sync_plus/consume'|grep -v grep|awk '{print \"kill -15 \"\$2}'|sh" done } restart() { printf "Restarting Worker QUEUE...\n" stop sleep 1 start } usage() { echo "Usage: sh $0 <start|stop|restart>" echo exit 1 } main#!/bin/bash help() { cat << HELP fork_push -- fork rabbitmq consumer php process USAGE: fork_push [-h] path count OPTIONS: -h help text EXAMPLE: ./fork_push.sh [or sh /data/www/api/bin/fork_push.sh] /createorder/consume 5 will fork 5 php process HELP exit 0 } error() { # print an error and exit echo "$1" exit 1 } while [ -n "$1" ]; do case $1 in -h) help;shift 1;; --) break;; -*) echo "error: no such option $1. -h for help";exit 1;; *) break;; esac done # input check: if [ -z "$1" ] ; then error "ERROR: you must specify a file, use -h for help" fi _path=$1 _count=$2 main() { if [ "${_path}" == "" ]; then usage fi if [ "${_count}" == "" ]; then _count=1 fi while true ; do PRO_NOW=`ps aux | grep $_path | grep -v grep | wc -l` if [ $PRO_NOW -le 2 -a $PRO_NOW -gt 0 ]; then let PRO_NOW=PRO_NOW-2 fi if [ $PRO_NOW -lt $_count ]; then _new_count=$[_count-PRO_NOW] for ((i=0; i<${_new_count}; i++)) do #线上使用 #nohup /opt/php/bin/php /opt/webroot/iyuansong/current/bin/sh.php ${_path} >/dev/null 2>&1 & #测试机用 nohup /opt/php/bin/php -q /opt/webroot/iyuansong/current/bin/sh.php ${_path} >/dev/null 2>&1 & done fi sleep 10 done } usage() { echo "Usage: sh $0 path <count>" echo exit 1 } main@session_start(); if(!defined('SESSION_START_STATUS')){ define('SESSION_START_STATUS','1'); } set_time_limit(0); ini_set("display_errors","On"); error_reporting(E_ALL); $_SERVER['VIA'] = "shell"; $_SERVER['PATH_INFO'] = ""; if(isset($_SERVER['argv'][1])){ $_SERVER['PATH_INFO'] = trim($_SERVER['argv'][1]); } $_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO']; if(isset($_SERVER['argv'][2])) { $_SERVER['REQUEST_URI'] = $_SERVER['REQUEST_URI']."?".trim($_SERVER['argv'][2]); } echo "start ".date("Y-m-d H:i:s", time())."\t"; echo $_SERVER['REQUEST_URI']."\tcontents:\n"; $appPath = realpath(dirname(dirname(__FILE__))); include($appPath."/index.php"); echo "\nend ".date("Y-m-d H:i:s", time()); -
幂等性校验
- 强校验
- 子订单
- 弱校验
- redis缓存
- 强校验
-
日志收集
- register_shutdown_function
- 注册一个会在php中止时执行的函数
- 可以多次调用 register_shutdown_function() ,这些被注册的回调会按照他们注册时的顺序被依次调用。 如果你在注册的方法内部调用 exit(), 那么所有处理会被中止,并且其他注册的中止回调也不会再被调用
- error_reporting
- 1 E_ERROR 致命的运行时错误。 错误无法恢复过来。脚本的执行被暂停
- 2 E_WARNING 非致命的运行时错误。脚本的执行不会停止
- 4 E_PARSE 编译时解析错误。解析错误应该只由分析器生成
- 8 E_NOTICE 运行时间的通知。该脚本发现一些可能是一个错误,但也可能发生在正常运行一个脚本
- 0 关闭错误报告
- register_shutdown_function
-
set_error_handler
- 设置用户自定义的错误处理函数
-
set_exception_handler
- 设置用户自定义的异常处理函数
- 设置默认的异常处理程序,用于没有用 try/catch 块来捕获的异常,在调用异常处理函数之后程序会终止
-
set_time_limit
- 设置脚本最大执行时间,最大的执行时间,单位为秒。如果设置为0(零),没有时间方面的限制。
-
ini_set('memory_limit','3000M');
-
判断脚本是否正在执行
keyword ) . " | grep -v grep | wc -l";
cmd );
- redis