eval在脚本中起作用,直到作为服务安装

时间:2018-07-04 21:21:19

标签: linux bash unix eval lsb

在Red Hat 7.4的init.d中,我有文件sapabi,该文件是通过调用安装的

  • chkconfig-添加sapabi

这将导致类似/etc/rc3.d /

的条目
  • S10network
  • S90sapinit
  • S91sapabi

/etc/rc6.d /

中的示例条目
  • K90network
  • K09sapabi
  • K10sapinit

当我以root用户身份执行命令时

  • ./ sapabi开始
  • 服务sapabi启动
  • 服务sapabi站

一切正常,并且sapcontrol按预期启动或停止了远程SAP诊断代理。

但是,当我输入“ reboot”命令时:

  • 关机期间根本不执行任何操作,甚至不执行'stop'参数
  • 在启动过程中,将start作为参数传递并调用start()函数。不幸的是,“ eval” sapcontrol命令没有像我调用./sapabi start命令时那样执行。相反,日志文件返回sapcontrol的“帮助”:

NAME sapcontrol(版本:753,补丁200,更改列表1844229)

简介sapcontrol [-prot] [-trace] [-debug] ...

  • 为什么在启动过程中不能像直接启动脚本那样正确地评估eval?
  • 为什么服务在关机期间没有停止?
  • 我在“必需开始”或“必需停止”中缺少什么吗?
  • 我不太确定在eval语句中正确使用'或'

非常感谢您的帮助

#!/bin/sh
#
# /etc/init.d/sapabi
#
# chkconfig: 345 91 09
# description: Start / stop SAP
#
### BEGIN INIT INFO
# Provides: sapabi
# Required-Start: $network $syslog $local_fs $named $remote_fs $time
# X-UnitedLinux-Should-Start:
# Required-Stop: $network $syslog $local_fs $named $remote_fs $time
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Start the sap system
# Description: Start the sap system
### END INIT INFO
PGM_PATH=$0
ARG1=$1
ARG2=$2

# Remote Diagnostics Agent configuration
#
# DA_R : If set to 1, remote diagnostics services will be started.
#
DA_R=1
DA_R_OS_USER="daaadm"
DA_R_OS_USER_PASSWORD="xxxxxx"
DA_R_INSTANCE_NR="98"
DA_R_START_CMD="StartSystem"
DA_R_STOP_CMD="StopSystem"
DA_R_HOST="xxx.xxx.xxx"

log=/var/log/sapabi.log

function ping_server()
{
    target=$1
    ping -c1 -w3 ${target} &>/dev/null && return 1 || return 0
}

function start_service()
{
    serviceName=$1
    localSidAdm=$2
    serviceHost=$3
    serviceUser=$4
    servicePassword=$5
    serviceInstance=$6
    serviceStartCmd=$7
    processName=$8
    waitTime=$9

    ping_server $serviceHost;
    if [ $? -eq 1 ];
    then
echo "in ping start_service $serviceName" >> $log
        eval ' su -s /bin/csh -l "$localSidAdm" -c "sapcontrol -nr "$serviceInstance" -host "$serviceHost"  -user "$serviceUser" "$servicePassword" -function "$serviceStartCmd""' >> $log
        test=""
        startTime=$(date +%s)
        currentTime=$(date +%s)
        while [[ "${test}" != *"GREEN"*  && ($((currentTime - startTime)) -lt $waitTime) ]];
            do
            test=`su -s /bin/csh -l "$localSidAdm" -c 'sapcontrol -nr '"$serviceInstance"' -host '"$serviceHost"' -user '"$serviceUser"' '"$servicePassword"' -function GetProcessList | grep '"$processName"''`
            # Do not print the trailing newline character
            echo -n "."
            sleep 1
            currentTime=$(date +%s)
        done
        echo ""
        echo "$serviceName - time $((currentTime - startTime)) seconds - ${test}" >> $log
    else
        echo "Error : ${serviceName} host ${serviceHost} not available"
        do_exit ${ERR_unknown_host}
    fi

    unset serviceName
    unset localSidAdm
    unset serviceHost
    unset serviceUser
    unset servicePassword
    unset serviceInstance
    unset serviceStartCmd
    unset processName
    unset waitTime
}

start() {
    #start the remote Diagnostics Agent
    if [ "$DA_R" == 1 ];
        then
            echo "Starting the remote Diagnostics Agent"
            start_service "Remote Diagnostics Agent" $LOCAL_SIDADM $DA_R_HOST $DA_R_OS_USER $DA_R_OS_USER_PASSWORD $DA_R_INSTANCE_NR $DA_R_START_CMD "jstart" "2700"
    fi
}
... similar stop functions ...

case "${ARG1}" in
    start )
        start 
    ;;
    stop )
        stop 
    ;;
    status )
        status 
    ;;
    restart )
        stop  
        start  
    ;;
    * )
        echo "Usage: ${PGM_PATH} {start|stop|status|restart}" 
        do_exit
    ;;
esac;

2 个答案:

答案 0 :(得分:0)

如果我正确阅读了您的代码,则可以使用eval创建一个日志条目。在这种情况下,替换eval巫婆回声“ ...”,并删除除开头和结尾引号外的所有引号。

答案 1 :(得分:0)

该问题与密码包含“#”符号有关。需要通过在“#”符号前加上“ \”来转义。