Linux脚本总结,近期linux shell脚本总结

crontab执行脚本时是不带任何环境变量的,用sh测试正常运行的脚本在布置成crontab定时任务后不一定能正常执行,需要在脚本里写设置环境变量的代码,并设置好脚本的工作目录。

export BASE_DIR=$HOME/test/softmon

passfile=$BASE_DIR/pass.conf

. $HOME/.bash_profile

cd $BASE_DIR

if语句结构,-f判断文件是否存在,注意[]里左右两侧的空格。

if [ ! -f $passfile ];then

echo 'passfile is not exist!'

exit

fi

格式化当前时间,并在当前时间的基础上进行时间加减运算。

datetime=`date "+%Y-%m-%d %H:%M:%S"`

today=`date "+%Y%m%d"`

yesterday=`date "+%Y%m%d" -d '-1 day'`

脚本里用sqlplus的-s参数连数据库,spool可将内容输出至文本,用to_char函数格式化查询date数据的输出,用||'|'||避免逐字段查询一条数据输出成多行,date - interval '15' minute可在数据库查询中对时间进行加减处理。

sqlplus -s $xdbapass <

set pages 0

set echo off

set feedback off

set line 2000

spool alert.lst

select alert_id||'|'||hostname||'|'||alert_type ||'|'||to_char(check_time,'yyyy-mm-dd hh24:mi:ss')||'|'||to_char((check_time - interval '15' MINUTE),'yyyy-mm-dd hh24:mi:ss')||'|'|| sysname||'|'||detail_message from test;

spool off

exit;

EOF

逐行读取文本文件内容,awk将字符分割后取特定字段

cat alert.lst|while read line

do

alert_id=`echo $line|awk -F '|' '{print $1}'`

hostname=`echo $line|awk -F '|' '{print $2}'`

alert_type=`echo $line|awk -F '|' '{print $3}'`

check_time=`echo $line|awk -F '|' '{print $4}'`

before_check_time=`echo $line|awk -F '|' '{print $5}'`

sysname=`echo $line|awk -F '|' '{print $6}'`

alert_message=`echo $line|awk -F '|' '{$1=$2=$3=$4=$5=$6=""; print $0}'`

today=`date -d "$check_time" "+%Y%m%d"`

yesterday=`date -d "$today-1day" "+%Y%m%d"`

...

done

文本内容作为url一部分时,用sed将空格替换成%20

sendmsg=`echo ${alert_message%[*}|sed 's/\ /\%20/g'`

phonelists=`echo $phonelist|sed 's/\ /\%20/g'`

mailx发送邮件,-r参数指定发件人邮箱,-s参数指定邮件主题

echo $datetime "warning:$alert_message"|mailx -r $sysname@triagen.com -s "$hostname $alert_type" $maillist

nohup &将命令置于后台执行,find命令后紧跟查询路径,-name匹配路径下文件,-newermt查找晚于给定时间的文件,-not -newermt查找早于给定时间的文件,-exec 对找出的文件名进行逐个处理,注意{} ;。

nohup find /$sysname/dump/$yesterday/ -name 'dump_GetClusterStacks*' -newermt "$before_check_time" -not -newermt "$check_time" -exec /usr/local/bin/python /$sysname/dump/dump_analayze.py -f {} \; &

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值