1、检查软件包是否安装
代码语言:javascript复制#!/bin/bash
if rpm -q sysstat &>/dev/null; then
echo "sysstat is already installed."
else
echo "sysstat is not installed!"
fi
2、安装软件脚本
根据Linux不同发行版使用不同的命令安装软件
代码语言:javascript复制#!/bin/bash
if [ -e /etc/redhat-release ]; then
yum install wget -y
elif [ $(cat /etc/issue |cut -d' ' -f1`) =="Ubuntu" ]; then
apt-get install wget -y
else
Operating system does not support.
exit
fi
3、检查服务状态
代码语言:javascript复制#!/bin/bash
PORT_C=$(ss -anu |grep -c 123)
PS_C=$(ps -ef |grep ntpd |grep -vc grep)
if [ $PORT_C -eq 0 -o $PS_C -eq 0 ]; then
echo "内容" | mail -s "主题" dst@example.com
fi
4、统计和分析Apache日志
通过apache访问日志access.log 统计IP和每个地址访问的次数,按访问量列出前10名。 日志格式样例如下: 192.168.1.247 ---【02/jul/2010:23:44:59 8080 】 "GET /HTTP/1/1" 200 19 答案: cat access.log | awk '{print $1}' |sort| uniq -c |sort -rn |head -10 (uniq 参数说明:– c 显示输出中,在每行行首加上本行在文件中连续出现的次数。 sort参数说明:sort默认的排序方式是升序,-r 参数就会改变成倒叙;你有没有遇到过10比2小的情况。我反正遇到过。出现这种情况是由于排序程序将这些数字按字符来排序了,排序程序会先比较1和2,显然1小,所以就将10放在2前面喽。这也是sort的一贯作风。)
5、Nginx启动脚本
代码语言:javascript复制#!/bin/bash
# Description: Only support RedHat system
. /etc/init.d/functions
WORD_DIR=/data/project/nginx1.10
DAEMON=$WORD_DIR/sbin/nginx
CONF=$WORD_DIR/conf/nginx.conf
NAME=nginx
PID=$(awk -F'[; ] ' '/^[^#]/{if($0~/pid;/)print $2}' $CONF)
if [ -z "$PID" ]; then
PID=$WORD_DIR/logs/nginx.pid
else
PID=$WORD_DIR/$PID
fi
stop() {
$DAEMON -s stop
sleep 1
[ ! -f $PID ] && action "* Stopping $NAME" /bin/true || action "* Stopping $NAME" /bin/false
}
start() {
$DAEMON
sleep 1
[ -f $PID ] && action "* Starting $NAME" /bin/true || action "* Starting $NAME" /bin/false
}
reload() {
$DAEMON -s reload
}
test_config() {
$DAEMON -t
}
case "$1" in
start)
if [ ! -f $PID ]; then
start
else
echo "$NAME is running..."
exit 0
fi
;;
stop)
if [ -f $PID ]; then
stop
else
echo "$NAME not running!"
exit 0
fi
;;
restart)
if [ ! -f $PID ]; then
echo "$NAME not running!"
start
else
stop
start
fi
;;
reload)
reload
;;
testconfig)
test_config
;;
status)
[ -f $PID ] && echo "$NAME is running..." || echo "$NAME not running!"
;;
*)
echo "Usage: $0 {start|stop|restart|reload|testconfig|status}"
exit 3
;;
esac
6、 检查MySQL主从同步状态
代码语言:javascript复制#!/bin/bash
USER=bak
PASSWD=123456
IO_SQL_STATUS=$(mysql -u$USER -p$PASSWD -e 'show slave statusG' |awk -F: '/Slave_.*_Running/{gsub(": ",":");print $0}') #gsub去除冒号后面的空格
for i in $IO_SQL_STATUS; do
THREAD_STATUS_NAME=${i%:*}
THREAD_STATUS=${i#*:}
if [ "$THREAD_STATUS" != "Yes" ]; then
echo "Error: MySQL Master-Slave $THREAD_STATUS_NAME status is $THREAD_STATUS!"
fi
done
7、MySQL数据库备份
代码语言:javascript复制#!/bin/bash
DATE=$(date %F_%H-%M-%S)
HOST=192.168.1.120
DB=test
USER=bak
PASS=123456
MAIL="zhangsan@example.com lisi@example.com"
BACKUP_DIR=/data/db_backup
SQL_FILE=${DB}_full_$DATE.sql
BAK_FILE=${DB}_full_$DATE.zip
cd $BACKUP_DIR
if mysqldump -h$HOST -u$USER -p$PASS --single-transaction --routines --triggers -B $DB > $SQL_FILE; then
zip $BAK_FILE $SQL_FILE && rm -f $SQL_FILE
if [ ! -s $BAK_FILE ]; then
echo "$DATE 内容" | mail -s "主题" $MAIL
fi
else
echo "$DATE 内容" | mail -s "主题" $MAIL
fi
find $BACKUP_DIR -name '*.zip' -ctime 14 -exec rm {} ;
8、监控JAVA进程
脚本内容:java_check.sh
#! /bin/bash # if [ -z `pidof java` ]; then echo -e `date %Y-%m-%d` `date %R`. “33[31m33[1m(JAVA)Process does not exist! 33[0m” >> /data/logs/java_check.log /usr/local/tomcat/bin/startup.sh >/dev/null 2>&1 sleep 30 netstat -tnlp >> /data/logs/java_check.log echo -e “r” >> /data/logs/java_check.log fi |
---|
计划任务,十五分钟执行一次
*/15 * * * * root /usr/local/bin/java_check.sh 一个监控java程序运行的shell
#!/bin/sh export J_LIB=/data/sms/lib; export CLASSPATH=.:$J_LIB/jtds-1.2.jar:$J_LIB/log4j-1.2.12.jar:$J_LIB/postgresql-8.1-404.jdbc3.jar; export JAVA_HOME=/usr/java/jdk1.5.0_05 export JRE=/usr/local/jdk/jre export SHELL=/bin/bash export HOME=/data/sms #java check status if /bin/ps ax | /bin/grep -v grep | /bin/grep "java XinTai"; then cd; else cd /data/sms/xintai; /usr/java/jdk1.5.0_05/bin/java XinTai >/dev/null & fi |
---|
定时执行可以保证 java XinTai可以一直在运行