@cyysu
2017-10-13T10:53:01.000000Z
字数 3084
阅读 886
- 时间:2017年10月13日
- 作者:Kali
- 邮箱:cyysu.github.io@gmail.com
- 版本:3.0
- 描述:启动脚本,控制协议栈进程的启动
脚本编写
#!/bin/bash#==============================================================================# Author : MJ_DZ chenyuan## Email : cyysu.github.io@gmail.com## Last modified : 2017-09-18 12:03## Filename : 桌面/startall.sh## Description : 启动协议栈以及上抛进程#==============================================================================# 相对路径readonly PROGDIR=$(readlink -f $(dirname $0))# 全局变量NEWTON=NEWTON_V2MQTT=SCADA_MQTTOPCUA=OPEN62541_V02MODBUS=LIBMODBUSparaCount=0paraArray=(0)# 1. Iterate through the incoming parameters and wrap the parameters into arrays# arg0: xml# arg1: processfor args in $@doparaArray[$paraCount]=$argslet paraCount++done# 2. 启动上抛数据进程,首先判断文件是否具有可执行权限,其次判断当前进程是否存在,然后判断XML文件是否存在function startProcess(){[ ! -x $PROGDIR/$1 ] && (chmod +x $PROGDIR/$1)if [ -f $PROGDIR/$1 ] && [[ -f $PROGDIR/${paraArray[0]} ]];then$PROGDIR/$1 ${paraArray[0]} #>/dev/null 2&>1 &fi}# 3. Read the XML configuration or manual input to see which process is thrownfunction findTcp(){# 如果参数大于/等于2 那么将会采取手动输入启动程序名字if [[ $2 -gt 2 ]];thenparamName=$3else# read xml config fileparamName=`grep "<test>" $1 | cut -d ">" -f 2 | cut -d "<" -f 1`fi# switch casecase $paramName inMODBUS)echo -e "\033[32m[INFO] 启动MODBUS进程\033[0m"startProcess $MODBUS;;OPCUA)echo -e "\033[32m[INFO] 启动OPCUA进程\033[0m"startProcess $OPCUA;;MQTT)echo -e "\033[32m[INFO] 启动MQTT进程\033[0m"startProcess $MQTT;;*)echo -e "\033[32m[INFO] 默认启动MQTT进程\033[0m"startProcess $MQTT;;esac}# 4. 程序看门狗function watchdog(){pidList=`ps -ef | grep $1 | grep -v grep | awk '{print $1}'`ps -ef | grep $1 | grep -v grepfor list in $pidListdokill -9 $list >/dev/null 2&>1#echo -e "\033[41m[INFO] 等待15S,如果未输入任何字符,程序将自行启动!!!\033[0m"#read -t 15 -p "请输入选项N/n: " input#if [[ $input == "n" || $input == "N" ]];then# exit 1#fidone}# 5. 程序状态function paraState(){#USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND#mj 8011 0.0 0.0 6764 776 pts/1 S+ 15:33 0:00 ./NEWTON_V2pidstate=`top -b -n 1 | grep $1 | grep -v grep | awk '{print $8}'`for file in $pidstatedoif [[ $file != "S+" || $file != "S" ]];thenecho findTcp $2 $3 $1watchdog $1 && findTcp $2 $3 $1fidone}# 清除上一次执行的所有状态watchdog $NEWTONwatchdog $OPCUAwatchdog $MQTTwatchdog $MODBUS# 调试接口,避免后台一直运行if [[ $1 == "clean" ]];thenif [[ ! -n "$2" ]];thenexitelsewatchdog $2exitfifi# 先启动上抛进程,然后启动底层协议栈if [[ $# -gt 0 ]];thenfindTcp $1 $# $2 &elseecho -e "\033[41m[ERROR] 请输入配置文件参数\033[0m"exitfi# 如果命令行参数大于2,也就是3个以上, 那么将会启动命令行中的应用# 如果参数为1或者2 都将会读取配置文件,如果无法匹配配置文件的内容那么将会默认启动MQTTecho -e "\033[32m[INFO] 启动NEWTON进程\033[0m"$PROGDIR/$NEWTON $PROGDIR/$1 &#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>以下为调试信息<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<#echo "USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND"#echo "========================================================================================="#ps -aux | grep NEWTON | grep -v grep#ps -aux | grep OPEN | grep -v grep#ps -aux | grep MODBUS | grep -v grep#ps -aux | grep MQTT | grep -v grep#echo "========================================================================================="#$PROGDIR/$OPCUA $PROGDIR/$1 &#$PROGDIR/$MODBUS $PROGDIR/$1 >/dev/null 2&>1 &#$PROGDIR/$MQTT $PROGDIR/$1 >/dev/null 2&>1 &# 7. 死循环程序的容错机制#while :#do# 查看协议栈进程状态#paraState $NEWTON $PROGDIR/$1 $# $2 &#paraState $OPCUA $PROGDIR/$1 $# $2 &#paraState $MODBUS $PROGDIR/$1 $# $2 &#paraState $MQTT $PROGDIR/$1 $# $2 &#done
支付宝 微信