Centos 7 redis、tomcat、Spring Boot添加开机自启服务

一、redis添加开机自启

1、添加服务配置文件

[root@test system]# vim /etc/systemd/system/redis-server.service

2、服务配置文件内容(前面红色字体是启动脚本,后面红色字体是配置文件);注意⚠️ 服务文件路径要写绝对路径

[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis.conf 
PrivateTmp=true [Install]
WantedBy=multi-user.target

3、重新加载服务的配置文件

[root@test system]# systemctl daemon-reload

4、启动redis服务

[root@test system]# systemctl start redis-server.service

5、设置开机自启

[root@test system]# systemctl enable redis-server.service

6、查看redis服务运行状态

[root@test system]# systemctl status redis-server.service
● redis-server.service - redis-server
   Loaded: loaded (/etc/systemd/system/redis-server.service; enabled; vendor preset: disabled)
   Active: active (running) since 五 2020-09-18 19:03:45 CST; 1h 53min ago
  Process: 899 ExecStart=/usr/local/bin/redis-server /etc/redis.conf (code=exited, status=0/SUCCESS)
 Main PID: 917 (redis-server)
   CGroup: /system.slice/redis-server.service
           └─917 /usr/local/bin/redis-server *:6379
9月 18 19:03:45 zx-001 systemd[1]: Starting redis-server...
9月 18 19:03:45 zx-001 systemd[1]: Started redis-server.

 二、Tomcat添加开机自启

1、添加服务配置文件

[root@test system]# vim /etc/systemd/system/tomcat-server.service

2、配置服务文件内容

[Unit]
Description=tomcat
After=network.target
[Service]
Type=forking
ExecStart=/www/server/tomcat/bin/startup.sh
ExecStop=/www/server/tomcat/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target

3、重新加载服务的配置文件

[root@test system]# systemctl daemon-reload

4、启动tomcat服务  

[root@test system]# systemctl start tomcat-server.service  

5、设置开机自启

[root@001 system]# systemctl enable tomcat-server.service

6、查看tomcat服务状态

[root@test system]# systemctl status redis-server.service
  • 有时候可能报错错误“Neither the JAVA_HOME nor the JRE_HOME environment variable is defined”
  • 原因服务启动没有状态jdk环境变量配置,有可能你没有在/etc/profile中配置,
  • 如果配置还是不起作用可以在tomcat/bin/下找到setclasspath.sh脚本在头部添加两行信息即可解决
  • export JAVA_HOME=/opt/jdk1.8.0_102/
    export JRE_HOME=/opt/jdk1.8.0_102/jre/ 
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk/
JAVA_JRE=/usr/lib/jvm/java-1.8.0-openjdk/jre/
PATH=$PATH:$JAVA_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME  CLASSPATH  PATH 
● tomcat-server.service - Internet of things platform
   Loaded: loaded (/etc/systemd/system/tomcat-server.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 五 2020-09-18 16:29:26 CST; 22s ago
  Process: 14373 ExecStart=/www/server/tomcat/bin/startup.sh (code=exited, status=1/FAILURE)
9月 18 16:29:26 test systemd[1]: Starting Internet of things platform...
9月 18 16:29:26 test startup.sh[14373]: Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
9月 18 16:29:26 test startup.sh[14373]: At least one of these environment variable is needed to run this program
9月 18 16:29:26 test systemd[1]: tomcat-server.service: control process exited, code=exited status=1
9月 18 16:29:26 test systemd[1]: Failed to start Internet of things platform.
9月 18 16:29:26 test systemd[1]: Unit tomcat-server.service entered failed state.
9月 18 16:29:26 test systemd[1]: tomcat-server.service failed.

三、SpringBoot添加开机自启

1、添加服务配置文件

[root@test system]# vim /etc/systemd/system/spring-boot-server.service

2、服务配置文件内容

[Unit]
Description=SpringBoot
After=network.target mariadb.target
[Service]
Type=forking
ExecStart=/usr/bin/java -jar /spring/spring-boot.jar 
ExecReload=/usr/bin/ps -ef | grep spring-boot.jar | grep -v grep | awk '{print $2}' | xargs kill -9 && java -jar/spring/spring-boot.jar
ExecStop=/usr/bin/ps -ef | grep spring-boot.jar | grep -v grep | awk '{print $2}' | xargs kill -9
Restart=always
[Install]
WantedBy=multi-user.target

3、重新加载服务的配置文件

[root@test system]# systemctl daemon-reload

4、启动SprinBoot服务

[root@test system]# systemctl start spring-boot-server.service

5、设置开机自启

[root@test system]# systemctl enable spring-boot-server.service

6、查看SprinBoot服务运行状态

[root@test system]# systemctl status spring-boot-server.service
● spring-boot-server.service - SpringBoot
   Loaded: loaded (/etc/systemd/system/spring-boot-server.service; enabled; vendor preset: disabled)
   Active: activating (start) since 五 2020-09-18 21:45:16 CST; 37s ago
  Control: 19563 (java)
   CGroup: /system.slice/spring-boot-server.service
           └─19563 /usr/bin/java -jar /spring/spring-boot.jar

9月 18 21:45:26 test java[19563]: 2020-09-18 21:45:26:522|INFO |main|springfox.documentation.spring.web.readers.operation.CachingOperationNameGenerator| Generating uniqu...geUsingGET_9
9月 18 21:45:26 test java[19563]: 2020-09-18 21:45:26:522|DEBUG|main|springfox.documentation.spring.web.readers.operation.OperationModelsProvider| Adding return paramete...mmon/Result;
9月 18 21:45:26 test java[19563]: 2020-09-18 21:45:26:523|DEBUG|main|springfox.documentation.spring.web.readers.operation.OperationModelsProvider| Reading parameters mod...Method |add|
9月 18 21:45:26 test java[19563]: 2020-09-18 21:45:26:523|DEBUG|main|springfox.documentation.spring.web.readers.operation.OperationModelsProvider| Finished reading param...Method |add|
9月 18 21:45:26 test java[19563]: 2020-09-18 21:45:26:523|DEBUG|main|springfox.documentation.swagger.readers.operation.SwaggerOperationModelsProvider| Reading parameters...Method |add|
9月 18 21:45:26 test java[19563]: 2020-09-18 21:45:26:523|DEBUG|main|springfox.documentation.spring.web.scanners.ApiModelReader| Generated parameter model id: Result, na...a: {} models
9月 18 21:45:26 test java[19563]: 2020-09-18 21:45:26:523|DEBUG|main|springfox.documentation.spring.web.scanners.ApiModelReader| Adding a new model with key Result
9月 18 21:45:53 iZwz9busydk4rcachg5ykzZ java[19563]: 2020-09-18 21:45:53:650|DEBUG|DefaultQuartzScheduler_QuartzSchedulerThread|org.quartz.core.QuartzSchedulerThread| batch acquisition of 1 triggers
9月 18 21:45:53 test java[19563]: 2020-09-18 21:45:53:667|DEBUG|quartzScheduler_QuartzSchedulerThread|org.quartz.core.QuartzSchedulerThread| batch acquisition of 0 triggers
Hint: Some lines were ellipsized, use -l to show in full.

补充服务配置文件

Description:描述服务 
After:描述服务类别 
[Service]服务运行参数的设置 
Type=forking是后台运行的形式 
ExecStart为服务的具体运行命令 
ExecReload为重启命令 
ExecStop为停止命令 
PrivateTmp=True表示给服务分配独立的临时空间 
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径 
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

 配置文件参考链接

 

posted @ 2020-09-18 21:54  天葬  阅读(636)  评论(1编辑  收藏  举报