sh格式文件,内容如下:
#!/bin/sh
DATE=$(date +%Y%m%d%H%M%S)
export JAVA_HOME=/usr/java/jdk1.6.0_23
LOG_FILE2="/usr/local/saas/backup/logs/worker2_"$DATE".out"
# stop the watchcat to prevent issues unexpectedly
#pkill watchcat.sh
#echo "==========shut down tomcat 2==========="
/usr/local/tomcat2/bin/shutdown.sh
sleep 10
#check if there're some tomcat2 ghosts not killed
tomcat2_pid=$(ps aux | grep -v grep | grep tomcat2 | awk '{print $2}')
if [ -n "$tomcat2_pid" ]
then
kill -9 "$tomcat2_pid"
echo "=====Finding and killing tomcat ghost(s) $tomcat2_pid completed===="
fi
sleep 50
#echo "==========backup worker 2 logs==========="
mv /usr/local/tomcat2/logs/catalina.out $LOG_FILE2
#echo "==========start up tomcat 2==========="
sleep 15
/usr/local/tomcat2/bin/startup.sh
sleep 10
#echo "==========shut down tomcat =========="
/usr/local/tomcat/bin/shutdown.sh
sleep 10
tomcat_pid=$(ps aux | grep -v grep | grep -w tomcat | awk '{print $2}')
if [ -n "$tomcat_pid" ]
then
kill -9 "$tomcat_pid"
echo "=====Finding and killing tomcat ghost(s) $tomcat_pid completed===="
fi
sleep 10
#echo "==========backup worker 1 logs==========="
LOG_FILE1="/usr/local/saas/backup/logs/worker1_"$DATE".out"
mv /usr/local/tomcat/logs/catalina.out $LOG_FILE1
sleep 50
#echo "===============start up tomcat =================="
/usr/local/tomcat/bin/startup.sh
# start watchcat daemon
#/home/yzm/watchcat.sh >> /home/yzm/cat_watching.log
难点解读:
ps aux | grep -v grep | grep tomcat2 | awk '{print $2}'
ps aux:显示所有程序(等同于ps -ef,两者只是显示结果的格式不一样)
|: 是管道符号,把|前面的命令执行结果作为后面命令的输入。
grep -v grep:过滤掉包含有grep字符的行
grep tomcat2:过滤掉不包含tomcat2字符的行
awk '{print $2}':awk是一个强大的文本分析工具,awk工作流程是这样的,读入有'\n'换行符分割的一条记录,然后将记录按指定的域分隔符划分域,填充域,$0则表示所有域,$1表示第一个域,$2表示第二个域(就是id),$n表示第n个域。默认域分隔符是"空白键" 或 "[tab]键",所以$2就是表示id。(如果要详细了解,见我的下一篇“awk文本分析工具解读”)
if [ -n "$tomcat2_pid" ]
then
kill -9 "$tomcat2_pid"
echo "=====Finding and killing tomcat ghost(s) $tomcat2_pid completed===="
fi
参考下面的例子
if [ -n "$1" ]
then
lines=$1
else
lines=$LINES
fi
判断目前的程序的位置参数$1是否非空 非空为真 空为假 非空把$1带入到lines里。如果为空就把上边定义过的$lines带入到lines里。
echo "=====Finding and killing tomcat ghost(s) $tomcat2_pid completed===="
echo命令的功能是在显示器上显示一段文字,一般起到一个提示的作用。
export JAVA_HOME=/usr/java/jdk1.6.0_23
在linux下设置环境变量,想具体了解环境变量配置,可以看我的另一篇“linux下设置环境变量”