玩命加载中🤣🤣🤣

service-register


服务注册

Linux

服务文件路径

  • /lib/systemd/system/
  • /usr/lib/systemd/system/

systemctl daemon-reload

Nginx案例

配置文件 nginx.service

[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target

注册脚本(参考)

#!/bin/bash
## -bash: ./initdee.sh: /bin/bash^M: bad interpreter: No such file or directory
## vim或者vi的命令模式下, 输入命令 set ff=unix 即可解决换行问题

cd /root
# 此处是nginx位置
nginxdir='/usr/local/nginx'
touch nginx.service
echo '[Unit]' >> nginx.service
echo 'Description=nginx - web server' >> nginx.service
echo 'After=network.target remote-fs.target nss-lookup.target' >> nginx.service
echo '' >> nginx.service
echo '[Service]' >> nginx.service
echo 'Type=forking' >> nginx.service
echo "PIDFile=$nginxdir/logs/nginx.pid" >> nginx.service
echo "ExecStartPre=$nginxdir/sbin/nginx -t -c $nginxdir/conf/nginx.conf" >> nginx.service
echo "ExecStart=$nginxdir/sbin/nginx -c $nginxdir/conf/nginx.conf" >> nginx.service
echo "ExecReload=$nginxdir/sbin/nginx -s reload" >> nginx.service
echo "ExecStop=$nginxdir/sbin/nginx -s stop" >> nginx.service
echo "ExecQuit=$nginxdir/sbin/nginx -s quit" >> nginx.service
echo 'PrivateTmp=true' >> nginx.service
echo '' >> nginx.service
echo '[Install]' >> nginx.service
echo 'WantedBy=multi-user.target' >> nginx.service
mv nginx.service /usr/lib/systemd/system
unset nginxdir


systemctl daemon-reload
systemctl enable nginx.service

Redis案例

#!/bin/bash
## -bash: ./initdee.sh: /bin/bash^M: bad interpreter: No such file or directory
## vim或者vi的命令模式下, 输入命令 set ff=unix 即可解决换行问题

cd /usr/local/redis*
redisdir=`pwd`
touch redis.service
echo '[Unit]' >> redis.service
echo 'Description=Redis' >> redis.service
echo 'After=syslog.target network.target remote-fs.target nss-lookup.target' >> redis.service
echo '' >> redis.service
echo '[Service]' >> redis.service
echo 'Type=forking' >> redis.service
echo 'PIDFile=/var/run/redis_6379.pid' >> redis.service
echo "ExecStart=$redisdir/src/redis-server $redisdir/redis.conf" >> redis.service
echo "ExecReload=$redisdir/src/redis-server -s reload" >> redis.service
echo "ExecStop=$redisdir/src/redis-server -s stop" >> redis.service
echo 'PrivateTmp=true' >> redis.service
echo '' >> redis.service
echo '[Install]' >> redis.service
echo 'WantedBy=multi-user.target' >> redis.service

mv redis.service /usr/lib/systemd/system

systemctl daemon-reload
systemctl enable redis.service
## 修改redis.conf 
sed -i 's/daemonize no/daemonize yes/g' redis.conf
sed -i 's/# supervised auto/supervised auto/g' redis.conf

cd ~
unset redisdir

Java项目案例(动态注册)

#!/bin/bash
## -bash: ./initdee.sh: /bin/bash^M: bad interpreter: No such file or directory
## vim或者vi的命令模式下, 输入命令 set ff=unix 即可解决换行问题

Windows

本案例采取winsw, 并且通过bat脚本进行服务的管理. 也可以通过服务管理器进行管理

所需文件

  1. winsw.exe 启动文件
  2. winsw.xml 配置文件

java 项目案例

注册服务

@echo off

echo -----------------------
echo Sentinel 一键安装程序
echo -----------------------

%~d0

cd %~dp0

echo Sentinel安装服务中
winsw install
echo Sentinel安装完成

pause

启动服务

@echo off

echo -----------------------
echo Sentinel 一键启动程序
echo ----------------------- 

net start Sentinel

pause

关闭服务

@echo off

echo -----------------------
echo Sentinel 一键关闭
echo -----------------------

:: 此处是服务名
net stop Sentinel

pause

卸载服务

@echo off

echo -----------------------
echo saddyfire Sentinel 一键卸载程序
echo -----------------------

%~d0

cd %~dp0

echo Sentinel卸载服务中
winsw uninstall
echo Sentinel卸载完成

pause

配置文件 xml

<service>
  <!-- 指定在Windows系统内部使用的识别服务的ID。在系统中安装的所有服务中,这必须是唯一的,它应该完全由字母数字字符组成 -->
  <id>Sentinel</id>
   <!-- 服务的简短名称,它可以包含空格和其他字符。尽量简短,就像“id”一样,在系统的所有服务名称中,也要保持唯一 -->
  <name>Sentinel</name>
  <!-- 该服务可读描述。当选中该服务时,它将显示在Windows服务管理器中 -->
  <description>Sentinel</description>
  <!-- 环境变量设置 -->
  <env name="JAVA_HOME" value="%JAVA_HOME%"/>
  <!-- 启动方式: 开机自启 -->
  <startmode>Automatic</startmode>
  <!-- 可执行文件传递的参数 -->
  <arguments>-Xrs -Xmx256m -jar "%BASE%\sentinel-dashboard-1.8.1.jar"  --server.port=9999</arguments>
  <!-- 该元素指定要启动的可执行文件 -->
  <executable>java</executable>
  <!-- 日志输出位置 -->
  <logmode>rotate</logmode>
</service>

nacos服务案例

服务注册

@echo off

echo -----------------------
echo nacos 一键安装程序
echo -----------------------

%~d0

cd %~dp0

:: 复制winsw.exe至 \bin
xcopy /Y /E /i winsw.exe ..\bin\
:: 复制winsw.xml 配置文件至 \bin
copy /Y winsw.xml ..\bin\
:: 进入 \bin
cd ..\bin\

echo saddyfire安装服务中
winsw install
echo saddyfire安装完成

pause

启动服务

@echo off

echo -----------------------
echo saddyfire 666 一键启动程序
echo ----------------------- 

net start Nacos

pause

关闭服务

@echo off

echo -----------------------
echo saddyfire 666 一键关闭
echo -----------------------

:: 此处是服务名
net stop Nacos

pause

卸载服务

@echo off

echo -----------------------
echo saddyfire 666 一键卸载程序
echo -----------------------

%~d0

cd %~dp0

echo Nacos卸载服务中
winsw uninstall
echo Nacos卸载完成

pause

配置文件 xml

<service>
	<!-- 指定在Windows系统内部使用的识别服务的ID。在系统中安装的所有服务中,这必须是唯一的,它应该完全由字母数字字符组成 -->
	<id>Nacos</id>
	<!-- 服务的简短名称,它可以包含空格和其他字符。尽量简短,就像“id”一样,在系统的所有服务名称中,也要保持唯一 -->
	<name>Nacos</name>
	<!-- 该服务可读描述。当选中该服务时,它将显示在Windows服务管理器中 -->
	<description>注册中心</description>
	<!-- 该元素指定要启动的可执行文件 -->
	<executable>%BASE%\startup.cmd</executable>
	<!-- 日志输出位置 -->
	<logmode>rotate</logmode>
</service>

PS: 普通命令启动Nacos

@echo off
:: 此处为nacos位置
start cmd /k "cd /d D:\develop\nacos-server-1.4.1\nacos\bin\ && startup.cmd -m standalone"
exit

文章作者: 👑Dee👑
版权声明: 本博客所有文章除特別声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 👑Dee👑 !
  目录