1. Run the Java program as the Linux daemon background
To monitor the network environment, a Java program is run on the Linux server. However, it is unrealistic to manually run each time you restart the instance. So I think of the Java program as an image of the Windows system service, and it will automatically run in the background at startup. I used a tool named ghost before. So I had to find another tool.
Finally, I found assumervice wrapper (http://wrapper.tanukisoftware.org/doc/english/introduction.html), which can be registered as a system service under many platforms (view support platforms ). There are three modes to use this tool. I use the first wrappersimpleapp help class. The other two methods both need to be properly encoded on the original program. The following describes the general configuration steps using my program netwatchdog as an example. (In fact, JBoss has been used for demonstration in the document)
1. Create a directory named netwatchdog and create the bin, Lib, Conf, and logs directories under the directory. The netwatchdog directory is called $ dog_home.
2. decompress the downloaded file and copy the $ wrapper_home/bin/wrapper, $ wrapper_home/src/bin/sh. Script. In file to the $ dog_home/bin directory.
3. Change the name of your service in the sh. Script. In file to netwatchdog.
4. Edit netwatchdog and modify the application information as follows:
APP_NAME="NetWatchDog"APP_LONG_NAME="NetWatchDog Application"
5. package Your program into a jar file, copy it to the $ dog_home/lib directory, and put $ wrapper_home/lib/libwrapper. so, $ wrapper_home/lib/wrapper. the jar file is also copied to the $ dog_home/lib directory.
6. this step is critical to success or failure. copy the conf file to the $ dog_home/conf directory. conf. but the downloaded file is wrapper. conf, so the step needs to be renamed ).
7. Edit the wrapper. conf file:
# Java command position wrapper. java. command = % java_home %/bin/Java # wrapper main class wrapper. java. mainclass = org. tanukisoftware. wrapper. wrappersimpleapp # Java classpathwrapper. java. classpath.1 = .. /lib/wrapper. jarwrapper. java. classpath.2 = % java_home %/JRE/lib/EXT/log4j-1.2.13.jarwrapper.java.classpath.3 = % java_home %/JRE/lib/EXT/activation. jarwrapper. java. classpath.4 = % java_home %/JRE/lib/EXT/commons-email-1.0.jarwrapper.java.classpath.5 = % java_home %/JRE/lib/EXT/mail. jarwrapper. java. classpath.6 = .. /lib/netwatchdog. jar # The main class of your program will be used as the wrapper parameter wrapper. app. parameter.1 = qiya. deng. main. main
8. Now you can run the command./netwatchdog start. If the prompt is insufficient, run the CHMOD 755 netwatchdog command to change the permission. If no error message is displayed, it is basically correct.
2. Use Java service wrapper to use Java program as a Linux service and start it automatically upon startup
In Linux, create the tianlong directory under/OPT, and copy tianlong. jar to the/opt/tianlong/lib directory.
1. download Java service wrapper (current version of wrapper-linux-x86-32-3.3.2), unzip.
Http://wrapper.tanukisoftware.org/
Copy src/bin/sh. Script. in to the/opt/tianlong/bin directory and rename it queuetest.
Copy the wrapper file under the wrapper/bin directory to the/opt/tianlong/bin directory.
Copy src/CONF/wrapper. conf. in to the/opt/tianlong/conf directory and rename it wrapper. conf.
Copy the three files in the src/lib directory to the/opt/tianlong/lib directory.
Wrapper. So
Wrapper. Jar
Wrappertest. Jar
2. Modify the/opt/tianlong/bin/queuetest file.
App_name = "queuetest" // note that the Application Service name here should be the same as the queuetest name in/opt/tianlong/bin in step 1.
App_long_name = "queuetest Application Server"
Wrapper_cmd = "./wrapper" // The path here must be correctly written. Here it corresponds to the/opt/tianlong/bin/wrapper file.
Wrapper_conf = "../CONF/wrapper. conf" // The path here must be correctly written. Here it corresponds to the/opt/tianlong/CONF/wrapper. conf file.
Grant execution permission
Chmod 775/opt/testapp/bin/socketserver
Chmod 775/opt/testapp/bin/wrapper
3. Modify the/opt/tianlong/CONF/wrapper. conf file.
Wrapper. java. Command =/usr/Java/jdk1.6.0 _ 07/bin/Java // here is the installation path for writing Java
Use wrapperstartstopapp to start/stop the program by running the command with start/stop.
Wrapper. java. mainclass = org. tanukisoftware. wrapper. wrapperstartstopapp
# Add the jar package file you used here, and write it all from 1.n, but wrapper is required.
Wrapper. java. classpath.1 = ../lib/wrapper. Jar
Wrapper. java. classpath.2 = ../lib/tianlong. Jar // here, it indicates to put the packaged tianlong. jar in the/opt/tianlong/lib folder.
Wrapper. java. classpath.3 = ../lib/asterisk-java-0.3.jar
Wrapper. java. classpath.4 = ../lib/mysql-connector-java-5.1.6-bin.jar
Wrapper. java. classpath.5 =/usr/Java/jdk1.6.0 _ 07/lib/dt. Jar
Wrapper. java. classpath.6 =/usr/Java/jdk1.6.0 _ 07/lib/tools. Jar
Wrapper. java. classpath.7 = ../cofig. properties // This is the configuration file used by the application. If the application needs to use the configuration, You need to include this file.
Wrapper. java. Library. path.1 = ../lib // indicates the path of the jar package used by wrapper. Generally, the default path is/opt/tianlong/lib.
# This is the main class (mainclass) of your executable program. Generally, it is the class that contains the public static void main.
Wrapper. App. parameter.1 = tianlong. queuetest
Wrapper. App. parameter.2 = 1
Wrapper. App. parameter.3 = true
Wrapper. App. parameter.4 = tianlong. queuetest
Wrapper. App. parameter.5 = true
Wrapper. App. parameter.6 = 1
Wrapper. App. parameter.7 = stop
4. Enable queuetest to run automatically upon startup
Ln-S/opt/tianlong/bin/queuetest/etc/init. d/queuetest
Ln-S/opt/tianlong/bin/queuetest/etc/rc0.d/queuetest
Ln-S/opt/tianlong/bin/queuetest/etc/rc1.d/queuetest
Ln-S/opt/tianlong/bin/queuetest/etc/rc2.d/queuetest
Ln-S/opt/tianlong/bin/queuetest/etc/rc3.d/queuetest
Ln-S/opt/tianlong/bin/queuetest/etc/rc4.d/queuetest
Ln-S/opt/tianlong/bin/queuetest/etc/rc5.d/queuetest
Ln-S/opt/tianlong/bin/queuetest/etc/rc6.d/queuetest
5. test execution command: Service queuetest START | stop | restart | status
When the program is running, Java service wrapper generates wrapper. Log logs in the/opt/tianlong/bin/directory,
Status | wrapper | 2009/02/25 17:04:01 | --> wrapper started as Daemon
Status | wrapper | 2009/02/25 17:04:01 | Java service wrapper community edition 3.3.2
Status | wrapper | 2009/02/25 17:04:01 | copyright (c) 1999-2009 tanuki software, Ltd. All rights reserved.
Status | wrapper | 2009/02/25 17:04:01 | http://wrapper.tanukisoftware.org
Status | wrapper | 2009/02/25 17:04:01 |
Status | wrapper | 2009/02/25 17:04:01 | launching a JVM...
Info | JVM 1 | 2009/02/25 17:04:02 | wrappermanager: initializing...
If the preceding figure appears, the configuration is successful. If the configuration is incorrect, a message is displayed in wrapper. log.
At the same time, if the program is successfully configured and started, there will be a queuetest. PID in/opt/tianlong/bin.
Attachment directory structure
/Opt/tianlong/
Cofig. Properties
/Opt/tianlong/bin
| Queuetest
| Wrapper
| Wrapper. Log (automatically generated when the program is running)
/Opt/tianlong/Conf
| Wrapper. conf
/Opt/tianlong/lib
| Libwrapper. So
| Wrapper. Jar
| Tianlong. Jar
Mysql-connector-java-5.1.6-bin.jar
Asterisk-java-0.3.jar