@cyysu
2017-10-13T10:53:01.000000Z
字数 3084
阅读 793
- 时间: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_V2
MQTT=SCADA_MQTT
OPCUA=OPEN62541_V02
MODBUS=LIBMODBUS
paraCount=0
paraArray=(0)
# 1. Iterate through the incoming parameters and wrap the parameters into arrays
# arg0: xml
# arg1: process
for args in $@
do
paraArray[$paraCount]=$args
let 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 thrown
function findTcp(){
# 如果参数大于/等于2 那么将会采取手动输入启动程序名字
if [[ $2 -gt 2 ]];then
paramName=$3
else
# read xml config file
paramName=`grep "<test>" $1 | cut -d ">" -f 2 | cut -d "<" -f 1`
fi
# switch case
case $paramName in
MODBUS)
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 grep
for list in $pidList
do
kill -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
#fi
done
}
# 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_V2
pidstate=`top -b -n 1 | grep $1 | grep -v grep | awk '{print $8}'`
for file in $pidstate
do
if [[ $file != "S+" || $file != "S" ]];then
echo findTcp $2 $3 $1
watchdog $1 && findTcp $2 $3 $1
fi
done
}
# 清除上一次执行的所有状态
watchdog $NEWTON
watchdog $OPCUA
watchdog $MQTT
watchdog $MODBUS
# 调试接口,避免后台一直运行
if [[ $1 == "clean" ]];then
if [[ ! -n "$2" ]];then
exit
else
watchdog $2
exit
fi
fi
# 先启动上抛进程,然后启动底层协议栈
if [[ $# -gt 0 ]];then
findTcp $1 $# $2 &
else
echo -e "\033[41m[ERROR] 请输入配置文件参数\033[0m"
exit
fi
# 如果命令行参数大于2,也就是3个以上, 那么将会启动命令行中的应用
# 如果参数为1或者2 都将会读取配置文件,如果无法匹配配置文件的内容那么将会默认启动MQTT
echo -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
支付宝 微信