1、编写shell脚本
# 服务名称
NAME='server'
# 指定配置文件
ARGS='-Dspring.profiles.active=zzd'
echo $NAME
PID=`ps -ef | grep "$NAME" | grep -v "$0" | grep -v "grep" | awk '{print $2}'`
echo $PID
echo "--start to kill $PID ----------"
for id in $PID
do
kill $id
echo "killed $id"
done
echo "--end kill $PID------------"
# 启动jar包
nohup java -Xms256m -Xmx2048m -XX:PermSize=128M -XX:MaxPermSize=256M $ARGS -jar /home/oa/server/$NAME.war 1>/dev/null 2>&1 &
注意这里的换行符需要用Unix系统的换行符
可以借助notepad++,双击截图中的位置选择Unix
截图上$ARGS这个参数的位置写错了,导致无法直接使用指定的配置文件启动
# 包位置
export package=$1
# 服务名称
export server_name=$2
# 配置文件 -Dspring.profiles.active=dev
export config_file=$3
export java_home=$(cat /etc/profile |grep JAVA_HOME |grep -v PATH |awk -F '=' '{print $2}')
# 停止服务
function kill_server_normal {
ps aux |grep $package |grep $server_name |grep -v start.sh |grep -v grep |awk '{print $2}'|while read id
do
kill -9 $id
echo "killed $id"
done
}
# 启动服务
function start_server_normal {
sleep 5
nohup nohup java -Xms256m -Xmx2048m -XX:PermSize=128M -XX:MaxPermSize=256M -jar $package/$server_name.jar 2>&1 &
sleep 5
}
kill_server_normal
echo "finish killed method"
start_server_normal
echo "server started"
使用方法
./start.sh /home/java weather -Dspring.profiles.active=dev
2、开机执行脚本
2.1修改Linux的开机启动配置文件(/etc/rc.d/rc.local)
vim /etc/rc.d/rc.local
2.2将脚本的全路径插入到开机启动配置文件里
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
/home/oa/server/start.sh