1:Shell脚本的编写
#!/bin/sh
export IOM=iom1.0.2-V2.0.0-exec.jar
export IEM=iem-V2.0.0-exec.jar
export POLICE=police-V2.0.0-exec.jar
export IOM_port=8114
export IEM_port=8116
export POLICE_port=9105
case "$1" in
start)
## 启动IOM
echo "--------IOM 开始启动--------------"
nohup java -jar $IOM >/dev/null 2>&1 &
IOM_pid=`lsof -i:$IOM_port|grep "LISTEN"|awk '{print $2}'`
until [ -n "$IOM_pid" ]
do
IOM_pid=`lsof -i:$IOM_port|grep "LISTEN"|awk '{print $2}'`
done
echo "IOM pid is $IOM_pid"
echo "--------IOM 启动成功--------------"
## 启动IEM
echo "--------开始启动IEM---------------"
nohup java -jar $IEM >/dev/null 2>&1 &
IEM_pid=`lsof -i:$IEM_port|grep "LISTEN"|awk '{print $2}'`
until [ -n "$IEM_pid" ]
do
IEM_pid=`lsof -i:$IEM_port|grep "LISTEN"|awk '{print $2}'`
done
echo "IEM pid is $IEM_pid"
echo "---------IEM 启动成功-----------"
## 启动POLICE
echo "--------开始启动POLICE---------------"
nohup java -jar $POLICE >/dev/null 2>&1 &
POLICE_pid=`lsof -i:$POLICE_port|grep "LISTEN"|awk '{print $2}'`
until [ -n "$POLICE_pid" ]
do
POLICE_pid=`lsof -i:$POLICE_port|grep "LISTEN"|awk '{print $2}'`
done
echo "POLICE pid is $GPOLICE_pid"
echo "---------POLICE 启动成功-----------"
echo "===startAll success==="
;;
stop)
P_ID=`ps -ef | grep -w $IOM | grep -v "grep" | awk '{print $2}'`
if [ "$P_ID" == "" ]; then
echo "===IOM process not exists or stop success"
else
kill -9 $P_ID
echo "IOM killed success"
fi
P_ID=`ps -ef | grep -w $IEM | grep -v "grep" | awk '{print $2}'`
if [ "$P_ID" == "" ]; then
echo "===IEM process not exists or stop success"
else
kill -9 $P_ID
echo "IEM killed success"
fi
P_ID=`ps -ef | grep -w $POLICE | grep -v "grep" | awk '{print $2}'`
if [ "$P_ID" == "" ]; then
echo "===POLICE process not exists or stop success"
else
kill -9 $P_ID
echo "POLICE killed success"
fi
echo "===stop success==="
;;
restart)
$0 stop
sleep 2
$0 start
echo "===restart success==="
;;
esac
exit 0
2:命令行
启动:./文件名.sh start
停止:./文件名.sh stop
重启:./文件名.sh restart
3:踩坑
1:进入./usr/local/java/文件名.sh下
执行./文件名.sh start
原因:这个脚本没有执行权限
解决方法: chmod +x .sh或chmod +x 文件名.sh
这里的u 这里指文件所有者,+x 添加可执行权限,.sh表示所有的sh文件。
2:
参考:https://www.sky8g.com/technology/2954/
解决方案
执行命令sed -i -e 's/\r$//' 文件名.sh