1.tomcat的启动脚本
#vi start_tomcat.sh
pid=`ps -ef |grep tomcat |grep -v grep|awk '{print $2}'` ##定义变量pid,等号两边不能有空格,另外``是Esc下面的两点,两点代表我执行的是命令
if[ $pid ] ##判断pid是否为真,非空即为真,中括号两边一定要有空格
then
echo "tomcat is running....byebye" ##echo后面一定要有空格
else
echo "tomcat is zhengzai qidong....."
sh /usr/local/tomcat/apache-tomcat-8.5.30/bin/startup.sh &&tail -f /usr/local/tomcat/apache-tomcat-8.5.30/logs/catalina.out #启动tomcat同时打印日志
fi
完整的日志如下:
但是做的过程还是遇到了一些问题:
第一次运行忘记写fi,程序报错“./start_tomcat1.sh: line 8: syntax error: unexpected end of file”
第二次,echo后面没有写空格就直接echo"tomcat is running ......byebye",程序报错“./start_tomcat1.sh: line 4: echotomcat is running...byebye: command not found”
2.tomcat的停止脚本#vi stop_tomcat.sh
3.启动的脚本
#vi restart_tomcat.sh
sh /tmp/stop_tomcat.sh
sh /tmp/start_tomcat.sh
经调试,以上脚本均能正常运行,不报错。