The Linux server restarted due to an unexpected power failure. No matter what you are doing, the company has to call you for urgent treatment, and many JAR projects need to be launched separately, which can be annoying. Simply solve a one-time solution to automatically start the JAR project service at startup. This will automatically run the project when the server restarts.

Microservice JAR startup script

Adding a shell Script

server.sh

#! /bin/bash # chkconfig: 2345 85 15 # description:auto_run # APP_HOME=/ WWW /wwwroot/test/ JAR_HOME_TOW=" 'CD ${APP_HOME} && find_name '*.jar''" APP_NAME = ${2} JAR_HOME_TOW: CD $APP_HOME # to check whether the program is running is_exist () {pid = ` ps - ef | grep $APP_NAME | grep -v grep | awk '{print $2}' If [-z "${pid}"]; Start (){is_exist if [$? -eq "0"]; then echo "=================================================" echo "warn: $APP_NAME is already running. (pid=$pid)" echo "=================================================" else nohup java -jar $APP_NAME > /dev/null 2>&1 & echo "${APP_NAME} start success" ${APP_NAME} stop(){is_exist if [$? -eq "0"]; then kill -9 $pid echo "${APP_NAME} stop success" else echo "=================================================" echo "warn: $APP_NAME is not running "echo" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = "fi} # output operation status () {is_exist if [ $? -eq "0" ]; then echo "=================================================" echo "warn: $APP_NAME is already running. (pid=$pid)" echo "=================================================" else echo "=================================================" echo "warn: $APP_NAME is not running "echo" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = "fi} # according to the input parameters, choose to perform the corresponding methods, Case "$1" in "start") start; "stop") stop ;; "status") status ;; "restart") stop echo "restart the application ..." start ;; *) echo "=================================================" echo "Tips: start|stop|restart|status" echo "=================================================" ;; esacCopy the code

Adding execute Permission

chmod +x  service.sh
Copy the code

Move it to the /etc/init.d directory

mv service.sh /etc/init.d/
Copy the code

【 Chkconfig startup mode 1 】

Chkconfig can update (start or stop) and query system service runlevel information. More simply, chkconfig is a command-line tool for maintaining the /etc/rc[0-6].d directory.Copy the code

Adding system Services

Chkconfig --add service.sh -- delete system service chkconfig --del service chkconfig --listCopy the code

Common commands

Sh stop # Restart the service service. Sh restart # Service status service service.sh statusCopy the code

【 SystemCTL boot from start mode 2 】

In Centos, the "systemctl" command is used to set the system service, which is called "service". It combines the previous functions of "service" and "chkconfig". You can use it to enable/disable services either permanently or only in the current session.Copy the code

Create service demo.service in /usr/lib/systemd/system/

[Unit]
Description=eureka
After=network.target
  
[Service]
Type=forking
ExecStart=/etc/rc.d/init.d/server.sh start
ExecReload=/etc/rc.d/init.d/server.sh restart
ExecStop=/etc/rc.d/init.d/server.sh stop
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target

Copy the code

Common commands

# Overloaded system service: Systemctl daemon-reload # set to start systemctl enable demo.service # Set to disable systemctl disable demo.service # Check the system service list Systemctl list-unit-files -type =service # Start the service systemctl start demo.service # Stop the service systemctl stop demo.service # Restart the service Systemctl reload demo.service # Service status systemctl status demoCopy the code