使用单个启动脚本启动多个进程并保持运行

时间:2016-11-09 13:38:58

标签: bash process background startup nohup

我想使用单个脚本在不同时间启动多个进程。但是,当我尝试使用它来启动第二个进程时,我没有考虑它已经运行的事实。所以我正在寻找能够帮助我实现这一目标的东西。

非常感谢任何帮助,如果需要更多信息,请告诉我....

离。我想做什么

./startup.sh QWER start

(稍后再次运行)

./startup.sh ASDF start 

基本上脚本接受2个参数,只检查以确保队列管理器存在,然后使用case语句来停止/启动/检查状态等。

下面的

脚本......

#!/bin/bash

#### Define vars #########################
qm=$1
QM=${qm^^}
stat=$2
proc=[I]ntroscopeAgent_$QM
MQMONITOR_BINDIR=/opt/bin/MQMonitor_$QM
#### Define New QUEUE MANAGERS ###########
declare -a QMGR=('QWER' 'ASDF' 'ZXCV' 'ASDF');
##########################################

if [[ " ${QMGR[@]} " =~ " $QM " ]]; then
case "$stat" in
  start)
        C=`ps -eo pid,args | grep "MQMonitor" | grep -c $proc`
        if [ ${C} -gt 0 ]; then
          proc_dtl=`ps -eo pid,args | grep "MQMonitor" | grep $proc`
          echo -e "\n\033[7mMQMonitor is running:\033[0m ${proc_dtl}\n\n\033[1mEither stop or restart the process\n\033[0m"
          exit 1
        fi
        echo -e "\nStarting MQMonitor MQ 9  "
        ${MQMONITOR_BINDIR}/startMQMonitor.sh &
        if [ $? -eq 0 ]; then
          echo -e "\033[0mMQMonitor Started.. (Please see start up log in \033[1m$logfile \033[0m) \n\033[0m"
        else
          echo -e "\033[01;31mMQMonitor ${MQMONITOR_BINDIR}/startMonitor.sh didn't start cleanly\n\033[0m"
        fi
        ;;
  stop)
        echo -e "\nStopping MQMonitor ..."
        ${MQMONITOR_BINDIR}/stopMQMonitor.sh
        if [ $? -eq 0 ]; then
          echo -e "\033[01;31mMQMonitor stopped\n\033[0m"
        else
          echo -e "\033[01;31mCouldn't stop MQMonitor\n\033[0m"
        fi
        ;;
  status)
        C=`ps -eo pid,args | grep "MQMonitor" | grep -c $proc`
        if [ ${C} -gt 0 ]; then
          PROC_ID=`ps -eo pid,args | grep "MQMonitor" | grep $proc | awk '{print $1}'`
          echo -e "\n\033[1m\033#6MQMonitor running PID: ${PROC_ID}\n\033[0m"
        else
          echo -e "\nMQMonitor not running\n"
        fi
        ;;
  restart|reload)
        C=`ps -eo pid,args | grep "MQMonitor" | grep -c $proc`
        if [ ${C} -gt 0 ]; then
          echo "MQMonitor Running..."
          echo "Stopping MQMonitor"
          ${MQMONITOR_BINDIR}/stopMQMonitor.sh
          if [ $? -eq 0 ]; then
            echo "MQMonitor stopped"
          else
            echo "Couldn't stop MQMonitor"
            exit 1
          fi
          echo "Stopped"
          echo "Starting MQMonitor"
          ${MQMONITOR_BINDIR}/startMQMonitor.sh &
          if [ $? -eq 0 ]; then
            echo "MQMonitor Started"
          else
            echo "MQMonitor ${MQMONITOR_BINDIR}/startMonitor.sh didn't start cleanly"
          fi
        else
          echo "MQMonitor not running"
          echo "Starting MQMonitor"
          ${MQMONITOR_BINDIR}/startMQMonitor.sh &
          if [ $? -eq 0 ]; then
            echo "MQMonitor Started"
          else
            echo "MQMonitor ${MQMONITOR_BINDIR}/startMonitor.sh didn't start cleanly"
          fi
        fi
        ;;
  *)
        echo -e $"\n\n\e[41mSYNTAX ERROR:\033[0m\tPlease Use Proper Syntax: QMGR {start|stop|status|restart}\n\e[41mEx:\033[0m\t\t./mqagent QPX1 status\n\n"
exit 1
esac
else
echo -e "\n\n\e[41mPlease enter a valid Queue Manager\033[0m\n\n"
fi

2 个答案:

答案 0 :(得分:0)

supervisord听起来对你有所帮助。

答案 1 :(得分:0)

通过取消设置位置参数,我可以在下次运行时传递新的位置参数。在末尾添加了一个移位命令以清除PP。