touch wso2mi.sh && chmod +x wso2mi.sh
#!/bin/bash MI_DATADIR=/usr/local/wso2mi/wso2mi-4.2.0 MI_LOG_FILE=/tmp/wso2mi.log DASHBOARD_DATADIR=/usr/local/wso2mi/wso2mi-dashboard-4.2.0 DASHBOARD_LOG_FILE=/tmp/wso2mi-dashboard.log # wso2的用户名 WSO2_USER=wso2 # wso2监听端口 WSO2_MI_PORT=8290 WSO2_DASHBOARD_PORT=9743 check_err() { if [ $? -ne 0 ]; then echo "$1" exit 1 else echo "$2" fi } start() { #检查是否存在netstat命令,没有则安装 type netstat &> /dev/null if [ $? -ne 0 ]; then yum install net-tools -y fi netstat -tln | grep ":$WSO2_MI_PORT" CHECK_RESULT=$? #取当前用户的变量值,$的前面要加反斜杠 if [ $CHECK_RESULT -eq 0 ];then echo "WSO2 MI 已启动,请勿重复启动应用!" exit 1 fi echo "正在启动 WSO2 MI....." # id -u为当前用户的uid,root用户uid为0,根据uid是否为0,判断当前用户是否是root userName=`whoami` if [ "$userName" != "$WSO2_USER" ]; then echo "当前用户为:$userName 用户,将切换为 $WSO2_USER 用户" else echo "当前用户为:$userName 用户" fi ip=`ifconfig ens33 | grep 'inet ' | sed 's/^.*inet //g' | sed 's/netmask.*$//g' | sed 's/ //g'` echo "当前服务器 IP 为:$ip" sed -i 's/localhost/'"${ip}"'/g' $MI_DATADIR/conf/deployment.toml sed -i '113,116s/# //g' $MI_DATADIR/conf/deployment.toml #userdel -r $WSO2_USER groupadd -g 10000 $WSO2_USER [ `id -u $WSO2_USER` ] || useradd -u 10001 -g $WSO2_USER -d /home/$WSO2_USER -m $WSO2_USER check_err "用户 $WSO2_USER 已存在!" "用户 $WSO2_USER 不存在,将创建新用户" if [ ! -f "$MI_LOG_FILE" ];then touch $MI_LOG_FILE chown -R $WSO2_USER:$WSO2_USER $MI_LOG_FILE chmod 755 $MI_LOG_FILE fi chown -R $WSO2_USER:$WSO2_USER $MI_DATADIR/ chmod 755 $MI_DATADIR chmod +x $MI_DATADIR/bin/micro-integrator.sh # 切换用户 su - $WSO2_USER << EOF # 不加反斜杠,取的值是切换用户前的 nohup $MI_DATADIR/bin/micro-integrator.sh >>$MI_LOG_FILE 2>&1 & #每5s检查一次监听端口是否启动,总共检查60次共10分钟,超时未启动则退出 CHECK_RESULT=1 CHECK_COUNT=60 while [ \$CHECK_RESULT -ne 0 ];do if [ \$CHECK_COUNT -gt 0 ];then echo "等待监听端口启动..." sleep 5 netstat -tln | grep ":$WSO2_MI_PORT" CHECK_RESULT=\$? CHECK_COUNT=\$((\$CHECK_COUNT-1)) else echo "ERROR: 启动$WSO2_MI_PORT端口超时,请检查!" exit 1 fi done echo "WSO2 MI 启动成功!" EOF netstat -tln | grep ":$WSO2_DASHBOARD_PORT" CHECK_RESULT=$? #取当前用户的变量值,$的前面要加反斜杠 if [ $CHECK_RESULT -eq 0 ];then echo "WSO2 DASHBOARD 已启动,请勿重复启动应用!" exit 1 fi echo "正在启动 WSO2 DASHBOARD....." if [ ! -f "$DASHBOARD_LOG_FILE" ];then touch $DASHBOARD_LOG_FILE chown -R $WSO2_USER:$WSO2_USER $DASHBOARD_LOG_FILE chmod 755 $DASHBOARD_LOG_FILE fi chown -R $WSO2_USER:$WSO2_USER $DASHBOARD_DATADIR/ chmod 755 $DASHBOARD_DATADIR chmod +x $DASHBOARD_DATADIR/bin/dashboard.sh # 切换用户 su - $WSO2_USER << EOF # 不加反斜杠,取的值是切换用户前的 nohup $DASHBOARD_DATADIR/bin/dashboard.sh >>$DASHBOARD_LOG_FILE 2>&1 & #每5s检查一次监听端口是否启动,总共检查60次共10分钟,超时未启动则退出 CHECK_RESULT=1 CHECK_COUNT=60 while [ \$CHECK_RESULT -ne 0 ];do if [ \$CHECK_COUNT -gt 0 ];then echo "等待监听端口启动..." sleep 5 netstat -tln | grep ":$WSO2_DASHBOARD_PORT" CHECK_RESULT=\$? CHECK_COUNT=\$((\$CHECK_COUNT-1)) else echo "ERROR: 启动$WSO2_DASHBOARD_PORT端口超时,请检查!" exit 1 fi done echo "WSO2 DASHBOARD 启动成功!" EOF #检查并关闭selinux CHECK_SELINUX=`getenforce` if [[ $CHECK_SELINUX != "Disabled" ]];then #临时关闭selinux setenforce 0 #配置文件关闭selinux,下次服务器重启后生效 sed -i '/^SELINUX=/s@.*@SELINUX=disabled@' /etc/selinux/config fi #检查防火墙是否已启动,如果启动则开放端口 service firewalld status > /dev/null if [ $? -eq 0 ];then firewall-cmd --add-port=8290/tcp --permanent firewall-cmd --add-port=8253/tcp --permanent firewall-cmd --add-port=9201/tcp --permanent firewall-cmd --add-port=9164/tcp --permanent firewall-cmd --add-port=9743/tcp --permanent firewall-cmd --reload fi } stop() { netstat -tln | grep ":$WSO2_MI_PORT" check_err "WSO2 MI 未启动!" "WSO2 MI 正在运行,开始停止应用....." su - $WSO2_USER -c "$MI_DATADIR/bin/micro-integrator.sh stop" CHECK_RESULT=0 CHECK_COUNT=60 while [ $CHECK_RESULT -eq 0 ];do if [ $CHECK_COUNT -gt 0 ];then echo "等待监听端口关闭..." sleep 5 netstat -tln | grep ":$WSO2_MI_PORT" CHECK_RESULT=$? CHECK_COUNT=$(($CHECK_COUNT-1)) else echo "ERROR: 关闭$WSO2_MI_PORT端口超时,请检查!" exit 1 fi done echo "关闭 WSO2 MI 成功!" netstat -tln | grep ":$WSO2_DASHBOARD_PORT" check_err "WSO2 DASHBOARD 未启动!" "WSO2 DASHBOARD 正在运行,开始停止应用....." su - $WSO2_USER -c "$DASHBOARD_DATADIR/bin/dashboard.sh stop" CHECK_RESULT=0 CHECK_COUNT=60 while [ $CHECK_RESULT -eq 0 ];do if [ $CHECK_COUNT -gt 0 ];then echo "等待监听端口关闭..." sleep 5 netstat -tln | grep ":$WSO2_DASHBOARD_PORT" CHECK_RESULT=$? CHECK_COUNT=$(($CHECK_COUNT-1)) else echo "ERROR: 关闭$WSO2_DASHBOARD_PORT端口超时,请检查!" exit 1 fi done echo "关闭 WSO2 DASHBOARD 成功!" } status() { su - $WSO2_USER -c "ps aux | grep $MI_DATADIR | grep -v \"grep\" | grep -v \"su $WSO2_USER\" | grep -v \"micro-integrator.sh\"" su - $WSO2_USER -c "ps aux | grep $DASHBOARD_DATADIR | grep -v \"grep\" | grep -v \"su $WSO2_USER\" | grep -v \"dashboard.sh\"" } restart() { stop start } usage () { echo " " echo "Please input start to start wso2mi." echo "Please input stop to stop wso2mi." echo "Please input status to get wso2mi status." echo "Please input restart to restart wso2mi." echo " " } INPUT_ACTIVE=$1 ACTIVE=${INPUT_ACTIVE:=start} case ${ACTIVE} in start) start ;; stop) stop ;; status) status ;; restart) restart ;; *) usage ;; esac