【问题标题】:Why does this script show an error on one server and not another with the same OS and shell version?为什么此脚本在一台服务器上显示错误,而不是在具有相同操作系统和 shell 版本的另一台服务器上显示错误?
【发布时间】:2019-03-06 20:33:46
【问题描述】:

我在多个 Web 服务器上安装了以下 tomcat 初始化脚本。

#!/bin/bash
#
# description: Apache Tomcat init script
# processname: tomcat
# chkconfig: 234 20 80
#
#
# Copyright (C) 2014 Miglen Evlogiev
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Initially forked from: gist.github.com/valotas/1000094
# Source: gist.github.com/miglen/5590986


#Location of JAVA_HOME (bin files)
export JAVA_HOME=/usr/java/latest

#Add Java binary files to PATH
export PATH=$JAVA_HOME/bin:$PATH

#CATALINA_HOME is the location of the bin files of Tomcat
export CATALINA_HOME=/opt/tomcat9

#CATALINA_BASE is the location of the configuration files of this instance of Tomcat
export CATALINA_BASE=/opt/tomcat9

#TOMCAT_USER is the default user of tomcat
export TOMCAT_USER=tomcat

#TOMCAT_USAGE is the message if this script is called without any options
TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;31mkill\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"

#SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
SHUTDOWN_WAIT=20

tomcat_pid() {
        echo `ps -fe | grep $CATALINA_BASE | grep -v grep | tr -s " "|cut -d" " -f2`
}

start() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
  else
    # Start tomcat
    echo -e "\e[00;32mStarting tomcat\e[00m"
    #ulimit -n 100000
    #umask 007

        if [ `user_exists $TOMCAT_USER` = "1" ]
        then
                sudo -u $TOMCAT_USER $CATALINA_HOME/bin/startup.sh
        else
                echo -e "\e[00;31mTomcat user $TOMCAT_USER does not exists. Starting with $(id)\e[00m"
                sh $CATALINA_HOME/bin/startup.sh
        fi
        status
  fi
  return 0
}

status(){
          pid=$(tomcat_pid)
          if [ -n "$pid" ]
            then echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
          else
            echo -e "\e[00;31mTomcat is not running\e[00m"
            return 3
          fi
}

terminate() {
    echo -e "\e[00;31mTerminating Tomcat\e[00m"
    kill -9 $(tomcat_pid)
}

stop() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo -e "\e[00;31mStoping Tomcat\e[00m"
    echo "DEBUG LINE 95"

      sudo -u tomcat $CATALINA_HOME/bin/shutdown.sh

    let kwait=$SHUTDOWN_WAIT
    count=0;
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
      echo -n -e "\n\e[00;31mwaiting for processes to exit\e[00m";
      sleep 1
      let count=$count+1;
    done

    if [ $count -gt $kwait ]; then
      echo -n -e "\n\e[00;31mkilling processes didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
      terminate
    fi
  else
    echo -e "\e[00;31mTomcat is not running\e[00m"
  fi

  return 0
}

user_exists(){
        if id -u $1 >/dev/null 2>&1; then
        echo "1"
        else
                echo "0"
        fi
}

case $1 in
    start)
      start
    ;;
    stop)
      stop
    ;;
    restart)
      stop
      start
    ;;
    status)
        status
        exit $?
    ;;
    kill)
        terminate
    ;;
    *)
        echo -e $TOMCAT_USAGE
    ;;
esac

两台服务器都运行相同的 Amazon Linux:

NAME="Amazon Linux AMI"
VERSION="2018.03"
ID="amzn"
ID_LIKE="rhel fedora"
VERSION_ID="2018.03"
PRETTY_NAME="Amazon Linux AMI 2018.03"
ANSI_COLOR="0;33"
CPE_NAME="cpe:/o:amazon:linux:2018.03:ga"
HOME_URL="http://aws.amazon.com/amazon-linux-ami/"

两台服务器运行相同的 bash 版本:

[ec2-user@host ~]$ echo $BASH_VERSION
4.2.46(2)-release

在一台服务器上,脚本正确设置 PID、创建 PID 文件并启动 Tomcat。当我停止 Tomcat 时,它会成功停止。

在另一台服务器上,脚本设置 两个 PID,不创建 PID 文件,并启动 Tomcat。当我停止 Tomcat 时,出现此错误:

/etc/init.d/tomcat9: line 100: [: =: unary operator expected

我知道如何纠正这个问题,并且有,但是为什么在两个主机上的行为不同,就所有意图和目的而言,都是相同的?它们都使用相同的 Ansible 剧本和角色进行配置,如图所示,它们全面运行相同版本的事物。那么是什么给出的呢?

【问题讨论】:

  • 我会先通过shellcheck.net 运行这个脚本,并在尝试进一步调试之前更正它指出的所有内容。
  • 另请注意,我们会询问包含代码的问题,以便将代码缩减为minimal reproducible example在询问之前。当您事先不知道哪些部分很重要时,sscce.org 上的“修剪技巧”指南是相关的。
  • ...也就是说,一般而言[: =: unary operator expected 表示预期会发出一个字符串拆分为单个单词的值,而不是在全部(正如shellcheck.net 所显示的那样,引用是错误的,所以我们得到了一个语法错误而不是优雅的处理)。
  • 选择user_exists比较,代码如果写成user_exists() { id -u "$1" &gt;/dev/null 2&gt;&amp;1; },然后if user_exists tomcat; then会简单很多;没有充分的理由在此处使用[ 命令
  • 您能否确认这一行是您从脚本获得的唯一输出,并且没有像Usage: grep [OPTION]... PATTERN [FILE]... 这样的其他错误消息?

标签: bash tomcat9


【解决方案1】:

我猜这是第 100 行:

until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]

显然进程 $pid 没有运行:grep 输出是 empty 并且 shell 看到这个:

until [  = '0' ] || [ $count -gt $kwait ]

由于 [ 命令现在只有 2 个参数,它会发出关于 = 是无效一元运算符的错误。

您需要引用所有参数,我建议使用 $() 而不是反引号:

until [ "$(ps -p $pid | grep -c $pid)" = '0' ] || [ $count -gt $kwait ]
# ..... ^^^ ....................... ^^

【讨论】:

  • 您可能想调查 pgrep 代替 ps | grep
  • ...OP 应该首先使用真正的进程监督系统(systemd、runit、upstart、launchd 等)而不是 SysV 风格的初始化脚本进行调查。
  • @CharlesDuffy 不幸的是,这些服务器暂时只能使用 Amazon Linux。当然,我想迁移到支持 systemd 的 Amazon Linux 2,但我们不能总是得到我们想要的。
猜你喜欢
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-24
相关资源
最近更新 更多