lrx182125 发表于 2018-8-23 12:13:57

shell script的追踪

  $ sh -n restart.sh
  $
  -n:不要执行script,仅仅检查语法,如果正确不会有任何输出,如果有错,则会有提示
  $ sh -x restart.sh
  + TOMCAT_HOME=/opt/apache-tomcat-8.0.14
  + cd /opt/apache-tomcat-8.0.14/bin
  ++ ps -ef
  ++ grep -v grep
  ++ wc -l
  ++ grep /opt/apache-tomcat-8.0.14
  + declare -i count=1
  + '[' 1 -eq 0 ']'
  + echo 'shutdown ......'
  shutdown ......
  + ./shutdown.sh
  Using CATALINA_BASE:   /opt/apache-tomcat-8.0.14
  Using CATALINA_HOME:   /opt/apache-tomcat-8.0.14
  Using CATALINA_TMPDIR: /opt/apache-tomcat-8.0.14/temp
  Using JRE_HOME:      /usr/java/jdk1.8.0_25

  Using>  ++ ps -ef
  ++ grep /opt/apache-tomcat-8.0.14
  ++ grep -v grep
  ++ wc -l
  + count=1
  + '[' 1 -ne 0 ']'
  ++ ps -ef
  ++ grep /opt/apache-tomcat-8.0.14
  ++ grep -v grep
  ++ awk '{print $2}'
  + declare -i pid=14433
  + kill 14433
  + echo 'tomcat is down'
  tomcat is down
  + ./startup.sh
  Using CATALINA_BASE:   /opt/apache-tomcat-8.0.14
  Using CATALINA_HOME:   /opt/apache-tomcat-8.0.14
  Using CATALINA_TMPDIR: /opt/apache-tomcat-8.0.14/temp
  Using JRE_HOME:      /usr/java/jdk1.8.0_25

  Using>  Tomcat started.
  + exit 0
  $
  -x:执行并输出script
  $ sh -v restart.sh
  #!/bin/bash
  TOMCAT_HOME=${TOMCAT_HOME:=/opt/apache-tomcat-8.0.14}
  cd $TOMCAT_HOME/bin
  declare -i count=`ps -ef|grep $TOMCAT_HOME|grep -v "grep"|wc -l`
  ps -ef|grep $TOMCAT_HOME|grep -v "grep"|wc -l
  if [ $count -eq 0 ];then
  echo "tomcat is not started"
  else
  echo "shutdown ......"
  ./shutdown.sh
  count=`ps -ef|grep $TOMCAT_HOME|grep -v "grep"|wc -l`
  if [ $count -ne 0 ];then
  declare -i pid=`ps -ef|grep $TOMCAT_HOME|grep -v "grep"|awk '{print $2}'`
  kill $pid
  fi
  echo "tomcat is down"
  fi
  ./startup.sh
  shutdown ......
  Using CATALINA_BASE:   /opt/apache-tomcat-8.0.14
  Using CATALINA_HOME:   /opt/apache-tomcat-8.0.14
  Using CATALINA_TMPDIR: /opt/apache-tomcat-8.0.14/temp
  Using JRE_HOME:      /usr/java/jdk1.8.0_25

  Using>  ps -ef|grep $TOMCAT_HOME|grep -v "grep"|wc -l
  ps -ef|grep $TOMCAT_HOME|grep -v "grep"|awk '{print $2}'
  tomcat is down
  Using CATALINA_BASE:   /opt/apache-tomcat-8.0.14
  Using CATALINA_HOME:   /opt/apache-tomcat-8.0.14
  Using CATALINA_TMPDIR: /opt/apache-tomcat-8.0.14/temp
  Using JRE_HOME:      /usr/java/jdk1.8.0_25

  Using>  Tomcat started.
  exit 0
  $
  -v:先输出,后执行,红色部分就是脚本,这个是tomcat重启的脚本

页: [1]
查看完整版本: shell script的追踪