一、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