shell脚本实例

来源:互联网 发布:视频矩阵的作用 编辑:程序博客网 时间:2024/06/07 02:10
代码如下:
<pre name="code" class="html">#!/bin/bash# clean job info from database which created 60 days ago# before clean data, get offset from bns, then delay [offset * 1800] s# if get offset failed. clean operation will not be executedreadonly MYSQL_HOSTNAME=xxreadonly PORT=xxreadonly USER=xxreadonly PASSWORD=xxreadonly TABLE=xx.xxreadonly TEST_BNS=xxreadonly PROXY_BNS=xxreadonly WORKSPACE=$(cd $(dirname $0); pwd)readonly LOCAL_HOSTNAME=`hostname`readonly HOSTNAME=${LOCAL_HOSTNAME/.baidu.com/}readonly DATE=-60dayreadonly CREATE_TIME="$(date -d $DATE '+%F %H:%M:%S')"readonly DEL_OPERATION="DELETE  FROM $TABLE WHERE create_time < '$CREATE_TIME' limit 1000"readonly SELECT_OPERATION="select count(*) as cnt FROM $TABLE WHERE create_time < '$CREATE_TIME'"#get bns offset, if failed, gen random number: [0-4]offset=-1multiply_factor=1800#record how many data left in databasecount=-1Prompt(){echo "[$(date '+%D %H:%M:%S')] $@"}ok() {    echo "[ok] ${1}"}#gen random number between [1, 5]randNumber() {    tmp=$RANDOM    offset=$[ tmp % 5 ]}delayTime() {    #here we cannot check $?, which is aways 0    tmp=$(get_instance_by_service "$PROXY_BNS" -o | grep "$HOSTNAME" | awk '{print $2}')    if [ "$tmp"x = ""x ]; then        Prompt "get bns offset failed, clean operation will not be executed.."        exit -1    else        offset=$tmp        Prompt "get bns offset success, bns offset: $offset"    fi}delOperation() {    mysql  -h $MYSQL_HOSTNAME -P$PORT -u$USER -p$PASSWORD -e "${DEL_OPERATION}"}countOperation() {    declare countTmp=`mysql  -h $MYSQL_HOSTNAME -P$PORT -u$USER -p$PASSWORD -e "${SELECT_OPERATION}"`    if [ "$countTmp"x = ""x ]; then        Prompt "count expired job failed, left clean operation will not be executed.."        exit -1    else        count=$(echo $countTmp | awk '{print $2}')        Prompt "count expired job success, [$count] job-data left"    fi}#only reserve data whose create_time within 60 dayscleanExpiredData() {    Prompt "select operation is $SELECT_OPERATION "    Prompt "delete operation is $SELECT_OPERATION "    Prompt "will del jobs where create_time < $CREATE_TIME"    countOperation    while [ $count -gt 0 ]    do        delOperation        countOperation    done}start() {    delayTime    if [ $offset -eq -1 ]; then        Prompt "get factor for delay time failed, please check.."        exit -1    else        ok "get factor for delay time success"    fi    sleeptime=$[ offset * multiply_factor ]    Prompt "clean mysql data after $sleeptime seconds .."    sleep $sleeptime    Prompt "wake up from sleep, start to clean data .."    cleanExpiredData    if [ $? -eq 0 ]; then        ok "clean data success."    else        Prompt "clean data failed .."        exit -1    fi}start


                                             
0 0
原创粉丝点击