Centos7 systemctl 使用自定义服务

版权声明:转载请链接 https://blog.csdn.net/DPnice/article/details/84099958
service目录:
/usr/lib/systemd/system
重新加载service文件:
systemctl daemon-reload
开机自启:
systemctl enable xxx

xxx为service文件名

启动:
systemctl start xxx.service
停止:
systemctl stop xxx.service
重启:
systemctl restart xxx.service
查看状态:
systemctl status xxx.service

status可以看到脚本的输出内容 pid 等

例1:
start-app.sh 内容:
#!/bin/bash
#author:DPnice

usage="Usage: start-app.sh start|stop|restart jar-path server-name server-port"
if [ $# -le 3 ]; then
	echo $usage
	#exit
	exit 1
fi

cmd=$1
jar_path=$2
service_name=$3
service_port=$4
APP_PID_DIR=/tmp/

case $cmd in
	start)
		java -jar "$jar_path" > "$jar_path.$service_name.log" &
		echo $! > "$APP_PID_DIR$service_name.$service_port.pid"
		if [ $? -eq 0 ]; then
			echo "Successful start"
			exit 0
		else
			echo "Startup failure"
			exit 1
	    fi
	;;
	stop)
		pid=$(cat $APP_PID_DIR$service_name.$service_port.pid)
		kill $pid && rm -rf "$APP_PID_DIR$service_name.$service_port.pid"
		if [ $? -eq 0 ]; then
			echo "Successful stop"
			exit 0
		else
			echo "Failure to stop"
			exit 1
		fi
		
	;;
	restart)
		pid=$(cat $APP_PID_DIR$service_name.$service_port.pid)
		kill $pid && rm -rf "$APP_PID_DIR$service_name.$service_port.pid"
		java -jar "$jar_path" > "$jar_path.$service_name.log" &
		echo $! > "$APP_PID_DIR$service_name.$service_port.pid"
		if [ $? -eq 0 ]; then
			echo "Successful restart"
			exit 0
		else
			echo "Restart the failure"
			exit 1
		fi
	;;
	*)
		echo "Error of parameters"
	;;
esac
对应的service内容:
[Unit]
Description=Control app
After=network.target remote-fs.target nss-lookup.target syslog.target

[Service]
Type=forking
ExecStart=/home/start-app.sh start /home/dpnice/Downloads/eureka-server-2.0.1.RELEASE.jar curd 7777
ExecReload=/home/start-app.sh restart /home/dpnice/Downloads/eureka-server-2.0.1.RELEASE.jar curd 7777
ExecStop=/home/start-app.sh stop /home/dpnice/Downloads/eureka-server-2.0.1.RELEASE.jar curd 7777
PrivateTmp=true

[Install]
WantedBy=multi-user.target
例2:
app-start.sh 内容:
#!/bin/bash
#author:DPnice

usage="Usage: app.sh start|stop|status jar-path server-name"
# if no args specified, show usage
if [ $# -le 2 ]; then
	echo $usage
	#exit
	exit 1
fi

#操作
cmd=$1
#jar path
jar_path=$2
#服务名称
service=$3

if [ ! -n "$jar_path" ] ;then
	echo "ERROR :Using the default jar packages path is null"
else
	#Exit if the argument is not a directory.
	if [ -d "$jar_path" ]
	then
		echo "ERROR : $jar_path is a directory"
		echo $usage
		exit 1
	else
		#export APP_PATH_DIR="$path_dir"
		echo "The jar packages path is $jar_path"
	fi
fi

#pid存放位置
if [ "$APP_PID_DIR" = "" ]; then
  APP_PID_DIR=/tmp/
fi

#启动一个jar的方法 入参:jar路径 服务名
start_jar() {
#启动命令 
new_pid=$APP_PID_DIR$service.pid
java -jar $1 > $1.$2.log &
echo $! > $new_pid

#Poll for up to more than 30 seconds for the java process to start
for i in {1..10}
do
	for i  in `ls $APP_PID_DIR`
	do
    	if [[ $i =~ $2 ]]
		then
		#echo $2 start success!
		return 0
		break
		fi
	done
sleep 3
done
#失败
return 1
}


case $cmd in
	start)
		#启动之前先判断是否已经启动
		for i  in `ls $APP_PID_DIR`
			do
				if [[ $i =~ $service ]]
				then
					echo Pid file : $APP_PID_DIR$i
					#判断进程是否运行
					oldpid=$(cat $APP_PID_DIR$i)
					if [[ $(ps -p "$oldpid" -o comm=) =~ "java" ]]; then
						echo "Application is running pid:$pid"
						exit 0
					else
						echo "The PID file exists, but the process does not exist."
						rm -rf $APP_PID_DIR$i
						echo "restart..."
						start_jar $jar_path $service
						
						if test $? -eq 0
						then
							echo "Success start $service"
							exit 0
						else
							echo "Failed start $service"
							exit 1
						fi
					fi
				fi
			done
		#pid文件不存在 正常启动
		start_jar $jar_path $service
		if test $? -eq 0
		then
			echo "Success start $service"
			exit 0
		else
			echo "failed start $service"
			exit 1
		fi
        ;;
	stop)
		echo "stop app..."
		for i  in `ls $APP_PID_DIR`
		do
			if [[ $i =~ $service ]]
			then
			oldpid=$(cat $APP_PID_DIR$i)
			kill $oldpid && rm -rf "$APP_PID_DIR$i"
			if [ $? -eq 0 ]; then
				echo "pid $oldpid stop success"
				exit 0
			else
				echo -e "pid $oldpid stop fail"
				exit 1
			fi
			break
			fi
		done

        ;;
	status)
		for i  in `ls $APP_PID_DIR`
		do
			if [[ $i =~ $service ]]
			then
				echo Pid file : $APP_PID_DIR$i
				oldpid=$(cat $APP_PID_DIR$i)
				if [[ $(ps -p "$oldpid" -o comm=) =~ "java" ]]; then
					echo "Application $service is running pid:$oldpid"
					exit 0
				else
					echo "The PID file($APP_PID_DIR$i) exists, but the process does not exist."
					rm -rf $APP_PID_DIR$i
					exit 0
				fi
			fi
		done
		echo "Application $service is not running"
		exit 0
        ;;
	*)
		echo "Error of parameters"
        ;;
esac

对应的service 内容:
[Unit]
Description=start all jar
After=network.target remote-fs.target nss-lookup.target syslog.target

[Service]
Type=forking
ExecStart=/home/app start /home/dpnice/Downloads/eureka-server-2.0.1.RELEASE.jar curd
ExecReload=/home/app start /home/dpnice/Downloads/eureka-server-2.0.1.RELEASE.jar curd
ExecStop=/home/app stop /home/dpnice/Downloads/eureka-server-2.0.1.RELEASE.jar curd
PrivateTmp=true

[Install]
WantedBy=multi-user.target

参考:
https://blog.csdn.net/xhzq1986/article/details/79761403

猜你喜欢

转载自blog.csdn.net/DPnice/article/details/84099958