概述
1. 设置ssh、scp命令的认证
运行一次脚本后,以后再使用ssh或scp命令登录远程机器时不再提示输入密码
-----------------setkey.sh开始-------------------------
OLDDIR=`pwd`
if [ -z "$1" ]; then
echo Need user@host info;
exit;
fi;
cd $HOME;
if [ -e "./.ssh/id_rsa.pub" ]; then
cat ./.ssh/id_rsa.pub | ssh $1 'mkdir -p .ssh;cat >> .ssh/authorized_keys';
else
ssh-keygen -t rsa;
cat ./.ssh/id_rsa.pub | ssh $1 'mkdir -p .ssh;cat >> .ssh/authorized_keys';
fi;
cd $OLDDIR
-----------------setkey.sh结束-------------------------
用法:setkey.sh [用户名]@[要登录的目标机IP地址]
2. 进程监控程序
定时检查进程,发现进程挂掉时重启进程
-----------------loopcheck.sh开始-------------------------
#!/bin/sh
predir=`pwd`
filedir=`dirname $0`
cd "${filedir}"
curdir=`pwd`
logfilename=./loopcheck.log
#-----------------------------------
# 函数: CheckProcess
# 功能: 检查一个进程是否存在
# 参数: $1 --- 要检查的进程名称
# 返回: 如果存在返回0, 否则返回1.
#---------------------------------------
checkprocess()
{
if [ "$1" = "" ]; then
return 1
fi
# $PROCESS_NUM获取指定进程名的数目,为1返回0,表示正常,不为1返回1,表示有错误,需要重新启动
process_num=`ps -ef |grep "$1" |grep -v "grep" |wc -l`
if [ $process_num -eq 1 ]; then
return 0
else
return 1
fi
}
# 启动C++进程
startBinProcessByName()
{
/bin/$1 > "log.${1}.txt" &
echo "`date` [startBinProcessByName]restart $1 ok" >> ${logfilename}
}
# 主逻辑
while [ 1 ]; do
# 对C++进程进行检查
tblBins=("gamesvr.bin" "dbsvr.bin")
for tblBin in "${tblBins[@]}";do
checkprocess "${tblBin} server${serverNo}"
check_result=$?
if [ $check_result -eq 1 ]; then
echo "`date` error ${tblBin}" >> ${logfilename}
startBinProcessByName "${tblBin}"
fi
done
# 每隔10秒执行一次
sleep 60
echo "`date` check ok" >> ${logfilename}
done
cd ${predir}
-----------------loopcheck.sh结束-------------------------
3. mysql数据库表自动修复脚本
-----------------checkdbtable.sh开始-------------------------
#!/bin/sh
mysql_host=localhost
mysql_user=star
mysql_pass=star
database=starcraft27
tables=$(mysql -h$mysql_host -u$mysql_user -p$mysql_pass $database -A -Bse "show tables")
for arg in $tables; do
check_status=$(mysql -h$mysql_host -u$mysql_user -p$mysql_pass $database -A -Bse "check table $arg" | awk '{ print $4 }')
if [ "$check_status" = "OK" ]
then
echo "$arg is ok"
else
echo $(mysql -h$mysql_host -u$mysql_user -p$mysql_pass $database -A -Bse "repair table $arg")
fi
# echo $(mysql -h$mysql_host -u$mysql_user -p$mysql_pass $database -A -Bse "optimize table $arg")
done
-----------------checkdbtable.sh结束-------------------------
4. mysql数据库备份脚本
-----------------backupdb.sh开始-------------------------
#!/bin/bash
predir=`pwd`
dir=`dirname $0`
cd "${dir}"
DB_DATABASE=databasename
DB_USR=username
DB_PWD=userpassword
BACKDIR=../backup/db
LOG_PATH=../backup/log
DATE=`date +%Y.%m.%d.%H.%M.%S`
targetFile=$BACKDIR/db.$DATE.tar.gz
if [ $# = 1 ];then
targetFile=$1
fi
mkdir -p $LOG_PATH
echo "backupdb begin">>${LOG_PATH}/dailyMaintenace.log
mkdir -p $BACKDIR
mysqldump -u"${DB_USR}" -p"${DB_PWD}" $DB_DATABASE >$DB_DATABASE.$DATE.sql
tar -zcf $targetFile $DB_DATABASE.$DATE.sql
rm -rf $DB_DATABASE.$DATE.sql
echo "backupdb done">>${LOG_PATH}/dailyMaintenace.log
cd "${predir}"
-----------------backupdb.sh结束-------------------------
5. mysql数据库备份恢复脚本
-----------------recoverdb.sh开始-------------------------
#!/bin/bash
predir=`pwd`
dir=`dirname $0`
cd "${dir}"
if [ $# = 0 ];then
echo please specified the source sql file for recover
exit 0
fi
if [ ! -e $1 ];then
echo "there isn't the file $1"
exit 0
fi
DB_DATABASE=databasename
DB_USR=username
DB_PWD=userpassword
RECOVERDIRTEMP=../backup/db/recovertmp
echo recovering...
mkdir -p $RECOVERDIRTEMP
rm -rf $RECOVERDIRTEMP/*
tar -xzf $1 -C $RECOVERDIRTEMP/
if [ $# = 2 ];then
mysql -u"${DB_USR}" -p"${DB_PWD}" -e "drop database if exists ${DB_DATABASE};create database ${DB_DATABASE};"
fi
mysql -u"${DB_USR}" -p"${DB_PWD}" $DB_DATABASE < $RECOVERDIRTEMP/*.sql
rm -f $RECOVERDIRTEMP/*.sql
echo done
cd "${predir}"
-----------------recoverdb.sh结束-------------------------
6. mysql数据库结果分析输出csv文件脚本
-----------------export_userinfo.sh开始-------------------------
#!/bin/bash
rm -rf export_out.csv
var=0
records=()
map=""
date
while read line
do
key=`echo $line | awk -F ' ' '{print $1}'`
#echo $key
result=(`mysql rxsg2_$1 --skip-column-names -e"select p.passport, u.name, u.level, o.total_online, u.vip from mem_passport p left join (mem_user u left join mem_user_online o on o.uid=u.uid) on u.uid=p.uid where p.passport='$key'"`)
len=${#result[@]}
if [ $len -ne 5 ]; then
records[$var]="$key"
else
records[$var]="${result[0]},${result[1]},${result[2]},${result[3]},${result[4]}"
fi
#echo ${records[$var]};
var=`expr $var + 1`
#if [ $var -eq 5 ]; then
#break;
#fi
done < export_in.txt
outstr="";
echo "over";
#echo ${records[*]} > export_result.txt
for ((i=0;i
do
echo ${records[$i]} >> export_out.csv
#outstr=$outstr`echo ${map[$i]}``echo "n"`
done
#echo $outstr > export_result.txt
date
-----------------export_userinfo.sh结束-------------------------
【参考】
http://www.cnblogs.com/end/archive/2011/07/20/2111888.html
本文转载出处:点击打开链接
最后
以上就是诚心鸡为你收集整理的linux的常用shell脚本语言,linux常用shell脚本的全部内容,希望文章能够帮你解决linux的常用shell脚本语言,linux常用shell脚本所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复