touch wso2am.sh && chmod +x wso2am.sh
#!/bin/bash DATADIR=/usr/local/wso2am-4.2.0 LOG_FILE=/tmp/wso2am.log # wso2的用户名 WSO2_USER=wso2 # wso2监听端口 WSO2_PORT=9443 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_PORT" CHECK_RESULT=$? #取当前用户的变量值,$的前面要加反斜杠 if [ $CHECK_RESULT -eq 0 ];then echo "WSO2 AM 已启动,请勿重复启动应用!" exit 1 fi echo "正在启动 WSO2 AM....." # 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' $DATADIR/repository/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 "$LOG_FILE" ];then touch $LOG_FILE chown -R $WSO2_USER:$WSO2_USER $LOG_FILE chmod 755 $LOG_FILE fi chown -R $WSO2_USER:$WSO2_USER $DATADIR/ chmod 755 $LOG_FILE # 切换为es用户 su $WSO2_USER << EOF # 不加反斜杠,取的值是切换用户前的 nohup $DATADIR/bin/api-manager.sh >>$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_PORT" CHECK_RESULT=\$? CHECK_COUNT=\$((\$CHECK_COUNT-1)) else echo "ERROR: 启动$WSO2_PORT端口超时,请检查!" exit 1 fi done echo "WSO2 AM 启动成功!" 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=8672/tcp --permanent firewall-cmd --add-port=8243/tcp --permanent firewall-cmd --add-port=8280/tcp --permanent firewall-cmd --add-port=9099/tcp --permanent firewall-cmd --add-port=9021/tcp --permanent firewall-cmd --add-port=9021/tcp --permanent firewall-cmd --add-port=11111/tcp --permanent firewall-cmd --add-port=9611/tcp --permanent firewall-cmd --add-port=9711/tcp --permanent firewall-cmd --add-port=38163/tcp --permanent firewall-cmd --add-port=9443/tcp --permanent firewall-cmd --add-port=8099/tcp --permanent firewall-cmd --add-port=9999/tcp --permanent firewall-cmd --add-port=8021/tcp --permanent firewall-cmd --add-port=5672/tcp --permanent firewall-cmd --add-port=9763/tcp --permanent firewall-cmd --reload fi } stop() { netstat -tln | grep ":$WSO2_PORT" check_err "WSO2 AM 未启动!" "WSO2 AM 正在运行,开始停止应用....." su $WSO2_USER -c "$DATADIR/bin/api-manager.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_PORT" CHECK_RESULT=$? CHECK_COUNT=$(($CHECK_COUNT-1)) else echo "ERROR: 关闭$WSO2_PORT端口超时,请检查!" exit 1 fi done echo "关闭 WSO2 AM 成功!" } status() { su $WSO2_USER -c "ps aux | grep $DATADIR | grep -v \"grep\" | grep -v \"su $WSO2_USER\" | grep -v \"$DATADIR/bin/api-manager.sh\"" } restart() { stop start } usage () { echo " " echo "Please input start to start wso2am." echo "Please input stop to stop wso2am." echo "Please input status to get wso2am status." echo "Please input restart to restart wso2am." echo " " } INPUT_ACTIVE=$1 ACTIVE=${INPUT_ACTIVE:=start} case ${ACTIVE} in start) start ;; stop) stop ;; status) status ;; restart) restart ;; *) usage ;; esac