【Linux】java应用的shell安装脚本

本文介绍了一个用于启动并配置web应用的小型Shell安装脚本。该脚本能够设置防火墙端口、停止旧服务、复制配置文件,并启动新的服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前做了一个功能(也是一个比较小的web应用),因为涉及到启动时可以添加一些配置,所以自己有写了个shell安装脚本。在这里简单记录一下:

#!/bin/bash

#Default install location is "/usr/local/phonebook". If you want to change install location, modify two paths: working directory below, and jar start path in start.sh

#enable 9993 port
#判断public.xml中是否已经enable9993端口,若无则在指定位置添加。注意转义字符
function enableFirewalld(){
	insertText="<description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.<\/description>"
	portText="  <port protocol=\"tcp\" port=\"9993\"\/>"
	publicXmlPath="/etc/firewalld/zones/public.xml"

	grep "9993" $publicXmlPath > /dev/null
	if [ $? -eq 0 ]; then
		echo -e "\nINFO: port 9993 has already enabled\n"
	else
		sed -i "/$insertText/a\\$portText" $publicXmlPath
		echo -e "\nINFO: enable phonebook port 9993 success\n"
	fi

	if ps -ef|grep "firewalld"|egrep -v grep >/dev/null
	then
		firewall-cmd --reload
		echo -e "\nINFO: restart firewalld success\n"
	fi
}

enableFirewalld

#该方法没有用到,这里仅是记录一下
function enableFirewalld2(){
	phoneBookPort=9993
	isFirewalldOpen=false
	isPortOpen=false
	while ( ! $isFirewalldOpen || ! $isPortOpen )
	do 
		if ps -ef|grep "firewalld"|egrep -v grep >/dev/null
		then
			isFirewalldOpen=true
			echo -e "\nINFO: start firewalld success\n"
			res=`firewall-cmd --zone=public --query-port=$phoneBookPort/tcp`
			if [ $res == "no" ]; then
				`firewall-cmd --zone=public --add-port=$phoneBookPort/tcp --permanent`
				`firewall-cmd --reload`
				enableFirewalld
			else
				isPortOpen=true
				echo -e "\nINFO: enable phonebook port 9993 success\n"
			fi
		else
			`systemctl start firewalld`
			enableFirewalld
		fi
	done
}

# stop old phonebook if exist
if ps -ef|grep "phonebook"|egrep -v grep >/dev/null
then
	`systemctl stop phonebook.service`
	echo -e "\nINFO: stop old phonebook.service success\n"
fi

#working directory
workingDirectory="/usr/local/phonebook"

#install directory
installDirectory=$workingDirectory"/install"
confDemo=$installDirectory"/application.yml"
serviceDemo=$installDirectory"/phonebook.service"
startShellDemo=$installDirectory"/start.sh"

#add shell permission
shellPath=$installDirectory"/*.sh"
`chmod +x $shellPath`

#copy phonebook.service
servicePath="/usr/lib/systemd/system"
serviceWantsPath="/etc/systemd/system/multi-user.target.wants"
serviceRoot=$servicePath"/phonebook.service"
serviceWantsRoot=$serviceWantsPath"/phonebook.service"

if [ -f "$serviceRoot" ]; then
	`rm -f $serviceRoot`
	echo -e "\nINFO: del old phonebook.service success\n"
fi
`cp $serviceDemo $servicePath`
sed -i "s#workingDirectory#$workingDirectory#g" $serviceRoot

#建立软链接
if [ -f "$serviceWantsRoot" ]; then
	`rm -f $serviceWantsRoot`
	echo -e "\nINFO: del old phonebook.service symlink success\n"
fi
`ln -s $serviceRoot $serviceWantsRoot`

#copy application.yml
confPath=$workingDirectory"/conf"
confRoot=$confPath"/application.yml"
if [ -f "$confRoot" ]; then
	`rm -f $confRoot`
	echo -e "\nINFO: del old application.yml success\n" 
fi
`cp $confDemo $confPath`

#param domain can not be null
echo -e "\nplease enter param domain(required):"
read domain

while [ ! -n "$domain" ]
do
	echo -e "\nplease enter param domain(required):"
	read domain
done

sed -i "s#domainParam#$domain#g" $confRoot

#param agentLists is optional
echo -e "\nplease enter param agentLists(optional.if null,start with default agentLists; otherwise, enter as follows: agent1,agent2,agent3):"
read agentLists

agentStr=""
if [ -n "$agentLists" ]; then
	agentStr=${agentLists//,/"\n        - "}
	agentStr="- "$agentStr
else
	agentStr="- SIP-T41S 66.84.254.158\n        - SIP-T46S 66.84.254.158\n        - SIP-T48S 66.84.254.158\n        - SIP-CP920 78.84.254.158\n        - SIP-T40P 54.84.254.158\n        - SIP-T19P_E2 53.84.254.158\n        - SIP-T54W 96.84.254.158\n        - SIP-T57W 97.84.254.42\n        - SIP-T27G 69.84.254.157"
fi

sed -i "s#agentListsParam#$agentStr#g" $confRoot

#xml store directory
xmlDirectory=$workingDirectory"/remotexml/"
sed -i "s#xmlDirectory#$xmlDirectory#g" $confRoot
#xml temp store directory
xmlTempDirectory=$workingDirectory"/remotexmltemp/"
sed -i "s#xmlTempDirectory#$xmlTempDirectory#g" $confRoot


# start phonebook
`systemctl enable phonebook.service`

`systemctl start phonebook.service`

if ps -ef|grep "phonebook"|egrep -v grep >/dev/null
then
	echo -e "\nINFO: phonebook start success\n"
else
	echo -e "\nINFO: phonebook start fail\n"
fi


目录结构大致如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值