Start java programs as a service in linux
1. Install jsvc
There is a jsvc.tar.gz file under the bindirectory of tomcat. Go to the bin directory of tomcat.
# Tar xvfz jsvc.tar.gz
# Cd jsvc-src
# Sh support/buildconf. sh
# Chmod 755 configure
#./Configure -- with-java =/usr/local/java (change to your JDK location)
# Make
2. Compile the Service Startup class
package com.sohu.jsvc.test;public class TestJsvc {public static void main(String args[]) {System.out.println("execute main method!");}public void init() throws Exception {System.out.println("execute init method!");}public void init(String[] args) throws Exception{System.out.println("execute init(args) method");}public void start() throws Exception {System.out.println("execute start method!");}public void stop() throws Exception {System.out.println("execute stop method!");}public void destroy() throws Exception{System.out.println("execute destroy method!");}}
|
The main method can be removed, but the init (String [] args), start (), stop (), destroy () methods cannot be fewer, when the service starts, it will first call the init (String [] args) method.
Call the start () method. when the service is stopped, the stop () method is called first, and then the destroy () method is called.
3. package this class into testjsvc. jar and put it in the/test directory.