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
- Linux shell 脚本实例
- shell脚本实例解析
- c shell 脚本实例
- shell脚本实例
- shell脚本实例备忘
- Linux shell 脚本实例
- Linux shell 脚本实例
- Linux shell 脚本实例
- shell脚本实例1
- shell脚本实例2
- shell脚本实例0
- shell脚本实例集合
- Shell脚本编程实例
- shell脚本实例
- shell脚本实例解析
- shell脚本实例参考
- Shell脚本实例
- shell脚本实例
- 关于JavaScript命名空间的一些心得
- 摩斯密码
- 史上全详细制作远程推送证书以及用极光进行远程推送教程
- 简单工厂,工厂方法
- java bridge
- shell脚本实例
- iOS内存管理方法
- ios md5加密大小写
- TREEset的模板类
- C++STL vector常见用法
- CTF——被改错的密码
- 15个数字的排序
- sql2008数据备份和还原,数据表导出导入
- 对ScrollPane的使用