[Practical Project] Jar package self-starting under Linux system

What is jar package self-starting

In Linux systems, "jar package self-starting" refers to packaging a Java program into an executable Jar file through configuration and setting it to run automatically when the system starts. The following are some concepts related to jar package self-starting:

Jar file: Jar (Java Archive) file is a file format used for packaging and distributing Java programs. It can contain Java class files, resource files, library files, etc. Jar files can use the Java Virtual Machine (JVM) to run the programs within them.

Executable Jar file: An executable Jar file refers to a Jar file with the entry point of the main program embedded in it. It can be run directly through Java commands or double-clicking. Jar files can be marked as executable by setting the Main-Class attribute in the manifest file.

Startup script: A startup script is a Shell script used to perform specific operations, such as setting environment variables, starting a Java virtual machine, and specifying the executable Jar file of the main program as a parameter. Startup scripts typically run Java programs as background processes.

System service: In Linux systems, Jar files can be managed as a system service so that they can run automatically when the system starts. A system service is a program that provides background functions and can be managed and controlled through system startup scripts or service management tools (such as Systemd).

Systemd: Systemd is an initialization system and service manager under the Linux system. It can be used to manage operations such as starting, stopping, restarting, and status viewing of system services. By creating and configuring a Systemd unit file, you can use the Jar file as a Systemd service and set it to run automatically when the system starts.

The benefits of self-starting jar packages

Automatically run when the system starts: By setting the Jar package as a self-starting service, you can ensure that the program automatically runs when the system starts. This is useful for applications that need to run as a background service after the system boots, without having to start them manually.

Convenient management and maintenance: After using the Jar package as a self-starting service, you can use relevant tools or commands to manage and maintain the service, such as starting, stopping, restarting, and status viewing. This can simplify operations and improve management efficiency.

Avoid manual intervention: Auto-starting services eliminates the need for manual intervention. You don't have to manually run the Jar package every time the system restarts, which reduces the risk of errors and improves system stability.

Startup sequence control: By configuring self-starting services, you can accurately control the startup sequence between different services. If your application depends on other services or resources, setting dependencies in a self-starting service can ensure the correct startup sequence.

Logging and error handling: Self-starting services can configure logging and error handling mechanisms. You can record program output information to a log file to facilitate troubleshooting and problem location. In addition, you can define error handling policies, including actions such as automatic restarts or sending notifications, to ensure service reliability.

By setting the Jar package as a self-starting service, you can easily manage and control the operation of the application and improve the reliability and stability of the system. This is especially true for background services, scheduled tasks, and applications that need to run automatically when the system starts.

Actual operation

Preliminary preparation

Check the version number of your server java

java -version

Insert image description here

Note: If the server here is the java11 version, the jdk environment in the jarSysInit.sh script below must be changed to the corresponding environment.

step

1. Create a new jar package self-starting file

sudo vi /etc/init.d/jarSysInit.sh

Press the i key to enter edit mode and enter the following:

# !/bin/bash
##JDK start 配置自己服务器的JDK环境(jkd8还是11)
export JAVA_HOME=/usr/java/jdk1.8.0_251-amd64
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar
export PATH=$PATH:$JAVA_HOME/bin
## JDK end
# 有多少个jar就后面加上去就好(这里需要参考自己jenkins的启动脚本注意全局变量$APP_PATH 要换成jar包对应的全路径)
nohup java -Xms1024m -Xmx1024m -jar /home/admin/apps/typing_provider.jar > /home/admin/apps/typing.log 2>&1 &

Press the esc key to exit the edit mode and enter: wq to save
Insert image description here
2. Edit the file that is automatically executed when the server is turned on.

sudo vi /etc/rc.d/rc.local

Press the i key to enter edit mode and add the following content
under touch /var/lock/subsys/local in this file :

#开机启动jar脚本
/etc/init.d/jarSysInit.sh

Insert image description here
Press the esc key to exit editing mode and enter: wq to save

3. Elevate permissions. Both script files are green.
Switch paths.

cd /etc/rc.d

Elevate privileges:

sudo chmod 755 /etc/rc.local

Check whether the upgrade is successful

ll

Insert image description here
Switch directory

cd /etc/init.d

Elevate privileges

sudo chmod 755 /etc/init.d/jarSysInit.sh

Insert image description here

Summarize

**When the Jar package is set as the self-starting service of the Linux system, the content can be further enriched, including:

System resource management: Through self-starting services, system resources, such as CPU, memory, and network, can be reasonably allocated and managed when the system starts. The program's resource usage can be optimized based on actual needs and ensure that other important tasks and services can run normally.

Security enhancement: By setting the program as a self-starting service, you can use a specific user identity or permissions to run the program to improve security. Access rights can be restricted and ensure that only authorized users can access and operate related services. This helps prevent unauthorized access and potential security breaches.

Monitoring and alarming: The self-starting service can be integrated with monitoring tools to monitor the running status of the program in real time. By configuring appropriate monitoring indicators and alarm rules, abnormal situations such as crashes, high loads, and memory leaks can be discovered and handled in a timely manner. This helps to take timely measures and guarantees the availability and stability of the service.

Automatic updates and version management: Using self-starting services, automatic updates and version management of programs can be achieved. You can set up a policy to regularly check for new versions and automatically download and deploy new Jar packages when updated. This ensures that applications always use the latest features and bug fixes, ensuring system security and stability.

Scalability and flexibility: With self-starting services, applications can be easily expanded if needed. You can add more parameters, configuration files, plug-ins, etc. to customize it according to actual needs. This provides greater flexibility and scalability for subsequent development and maintenance of applications.

To sum up, setting the Jar package as a self-starting service in the Linux system not only simplifies the operation process, improves reliability and management efficiency, but also provides more functions and guarantees, such as resource management, security enhancement, monitoring and alarming. , automatic updates and flexibility, etc. By taking advantage of these advantages, you can better manage and run your applications to meet changing needs and deliver a superior user experience. **

Guess you like

Origin blog.csdn.net/weixin_45309155/article/details/133268898