通过systemctl/shell脚本一键启动和停止各种服务,提升软件交付效率。
1.启动
#!/bin/bash export JAVA_HOME="/mingde/jdk" export PATH=$JAVA_HOME/bin:$PATH ServiceName=$1 if [ ! -n "$ServiceName" ]; then echo "##################请输入要启动的服务名称##################" && exit 0 else echo "###################正在启动${ServiceName}服务################################" fi CMD_PATH="/mingde/${ServiceName}" if [[ ! -d "$CMD_PATH" ]]; then rm -rf echo "##############${CMD_PATH}不存在############" && exit 0 else cd $CMD_PATH/ fi chmod 755 ${CMD_PATH}/${ServiceName}.jar nohup java -jar ${CMD_PATH}/${ServiceName}.jar & echo $! > $CMD_PATH/logs/${ServiceName}.pidstartProject.sh
2.停止
#!/bin/bash set -e ServiceName=$1 if [ ! -n "$ServiceName" ]; then echo "##################请输入要停止的服务名称################" && exit 0 else echo "###################正在停止${ServiceName}服务################################" fi CMD_PATH="/mingde/${ServiceName}" if [[ ! -d "$CMD_PATH" ]]; then rm -rf echo "##############${CMD_PATH}不存在############" && exit 0 else cd $CMD_PATH/ fi PID=$(cat $CMD_PATH/logs/${ServiceName}.pid) if [ ! -n "$PID" ]; then echo "#####PID为空#####" && exit 0 else echo "当前PID为${PID}" fi kill -9 $PID || echo "#############PID为${PID}的进程已经关闭############" rm -f nohup.outstopProject.sh
3.systemd
[Unit] Description=######神华项目###### After=syslog.target network.target [Service] Type=forking Environment=JAVA_HOME="/mingde/jdk" ExecStart=/mingde/conf/startProject.sh "shenhua" ExecStop=/mingde/conf/stopProject.sh "shenhua" #ExecReload=/mingde/conf/restartProject.sh "shenhua" PrivateTmp=true User=root Group=root [Install] WantedBy=multi-user.targeshenhua.service