linux服务一键部署

场景

当你在linux中部署tomcat或springBoot服务的时候,每次服务的重启都需要跳转到服务所在的目录,执行不同的启动脚本。
以下是针对tomcat服务和springboot服务记录如何制作一个服务启动脚本的操作步骤。

服务依赖

jdk: jdk-8u181-linux-x64.tar.gz
mysql: mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz

jdk

jdk服务解压到目录[ /usr/local/deploy/datafs/jdk ]即可

#/bin/bash
echo "start install jdk"

basename=`pwd`

echo "${basename}"

if [ ! -d /usr/local/deploy/datafs/jdk ]
then
   echo "mkdir jdk"
else
   rm -rf /usr/local/deploy/datafs/jdk
fi

mkdir -p /usr/local/deploy/datafs/jdk

tar -zxvf jdk-8u181-linux-x64.tar.gz -C /usr/local/deploy/datafs/jdk

chmod -R 774 /usr/local/deploy/datafs/jdk

mysql

#!/bin/bash 
echo 'start install mysql.'

#检查mysql依赖的perl是否存在

yum install -y perl-Module-Install.noarch

#检查mysqld服务状态并停止

process=`ps -e|grep mysqld`

for i in $process
do
   echo "kill mysqld $i"
   kill -9 $i
done
  
#读取当前目录下的mysql包 
datamenu=`pwd`
echo ${datamenu}
mysql_port=3306
mysqlfile="mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz"


#创建一个mysql目录,将tar解压到mysql 
if [ ! -d '/usr/local/deploy/datafs/mysql' ] ; then
   echo 'mkdir mysql'
else
   echo 'uninstall mysql'
   rm -rf /usr/local/deploy/datafs/mysql
fi

mkdir -p /usr/local/deploy/datafs/mysql

tar -zxvf $mysqlfile -C /usr/local/deploy/datafs/mysql

egrep "^mysql" /etc/group >& /dev/null
if [ $? -ne 0 ] 
then 
  groupadd mysql
fi

egrep "^mysql" /etc/passwd >& /dev/null
if [ $? -ne 0 ]
then 
  useradd -g mysql -s /sbin/nologin -d /usr/local/mysql mysql
fi

rm -rf /usr/local/mysql
ln -s /usr/local/deploy/datafs/mysql/mysql-5.6.41-linux-glibc2.12-x86_64 /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql/

if [ ! -d '/data/mysql' ]
then 
	rm -rf /data/mysql
fi

mkdir -p /data/mysql
mkdir -p /data/mysql/mysql${mysql_port}

mkdir /data/mysql/mysql${mysql_port}/data
mkdir /data/mysql/mysql${mysql_port}/logs
mkdir /data/mysql/mysql${mysql_port}/tmp
chown -R mysql:mysql /data/mysql/mysql${mysql_port}

cat > /etc/my.cnf << EOF

[mysqld]
port = ${mysql_port}
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql/mysql${mysql_port}/data
tmpdir = /data/mysql/mysql${mysql_port}/tmp
socket = /tmp/mysql.sock

log-bin=/data/mysql/mysql${mysql_port}/logs/mysql-bin

log-error=error.log
slow-query-log-file=slow.log
skip-external-locking
skip-name-resolve
log-slave-updates

lower_case_table_name = 1

character_set_server=utf8
innodb_file_per_table = 1
innodb_autoinc_lock_mode=2

explicit_defaults_for_timestamp = true
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

EOF

cd /usr/local/mysql
./scripts/mysql_install_db --defaults-file=/etc/my.cnf

if [ "$?" -ne 0 ]
then 
   echo "run ./script/mysql_install_db error"
   exit 1
fi

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

echo "export PATH=$PATH:/usr/local/mysql/bin">>/etc/profile  

source /etc/profile

chmod +x /etc/init.d/mysqld
chkconfig --add mysqld

cd $datamenu

SpringBoot服务

#!/bin/sh
#chkconfig: 2345 80 05
#description: api-server.jar

ServerName=api-server
JarPath=/usr/local/deploy/datafs/api-server

start()
{
   count=`ps -ef | grep java|grep $ServerName|grep -v grep|wc -l`
   if [ $count != 0 ]; then
	  echo "$ServerName is running..."
   else
	  echo "Startup $ServerName..."
	  cd ${JarPath}
	  export LC_ALL=zh_CN.UTF­8
	  nohup /usr/local/deploy/datafs/jdk/jdk1.8.0_181/bin/java -jar ${JarPath}/${ServerName}.jar > output 2>&1 &
	  sleep 2
	  status=`ps -ef | grep java|grep $ServerName|grep -v grep|wc -l`
	   if [ $status != 0 ]; then
		  echo "start $ServerName success..."
	   else
		  echo "start $ServerName failed..."
	   fi
   fi
}

stop()
{
  echo "Stop $ServerName"
   boot_id=`ps -ef | grep java|grep $ServerName|grep -v grep|awk '{print $2}'`
   count=`ps -ef | grep java|grep $ServerName|grep -v grep|wc -l`

   if [ $count != 0 ];then
	  echo "pid is $boot_id"
	  kill $boot_id
	  count=`ps -ef | grep java|grep $ServerName|grep -v grep|wc -l`

	  boot_id=`ps -ef | grep java|grep $ServerName|grep -v grep|awk '{print $2}'`
	  kill -9 $boot_id
   fi
}

status()
{
   count=`ps -ef | grep java|grep $ServerName|grep -v grep|wc -l`
   if [ $count != 0 ];then
	   echo "$ServerName is running..."
   else
	  echo "$ServerName is not running..."
   fi
}

case $1 in
start)
   start
   ;;
stop)
   stop
   ;;
status)
   status
   ;;
restart)
   stop
   sleep 1
   start
   ;;
*)
   echo "api-server Usage :"
   echo "service api-server start|stop|restart|status"
   ;;
esac

Tomcat服务

#!/bin/bash
# description: Tomcat Start Stop Restart
# processname: ecsweb
# Required-Start: $local_fs $network $remote_fs $mysqld
# chkconfig:234 20 90

CATALINA_HOME=/usr/local/deploy/datafs/web/

cd $CATALINA_HOME/bin

case $1 in

	 start)
		c=`ps -ef | grep "/usr/local/deploy/datafs/web" | grep -v grep | wc -l`
		if [ $c -eq 1 ]
		then
			echo "ecsweb is Running, Please execute stop and start."
			exit 1
		fi
	sh ./catalina.sh start
		echo "ecsweb is start..."
		sleep 2
	;;	

	 status)
	c=`ps -ef | grep "/usr/local/deploy/datafs/web" | grep -v grep | wc -l`
		if [ $c -eq 1 ] 
		then
			echo "ecsweb is Running"
		else
			echo "ecsweb is Not Running"
		fi
	;;
		
	 stop)
	   echo "stop ecsweb..."
	   pid=`ps -ef | grep "/usr/local/deploy/datafs/web" | grep -v grep | awk '{print $2}'`
	   for i in $pid
	   do
		   kill -9 $i
	   done
	   sleep 1
	   ;;
	 *)
	echo "please use : ecsweb {start | stop}"
		;;
esac
exit 0

你可能感兴趣的:(linux服务一键部署)