Use jsvc to embed java programs into linux services
Source: Internet
Author: User
Use jsvc to embed a java program into a linux service-Linux general technology-Linux programming and kernel information. The following is a detailed description. 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! ");
}
The main method can be removed, but the init (String [] args), start (), stop (), destroy () methods cannot be fewer, the service first calls the init (String [] args) method at startup.
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.
4. write the script for starting the service myjsvc
#! /Bin/sh
# Myjsvc This shell script takes care of starting and stopping
#
# Chkconfig:-60 50
# Description: tlstat stat is a stat data daemon.
# Processname: myjsvc
# Source function library.
./Etc/rc. d/init. d/functions
# For multi instances adapt those lines.
TMP_DIR =/var/tmp
PID_FILE =/var/run/tlstat. pid
# Program running is required jar package, commons-daemon.jar is not less
CLASSPATH = \
/Test/testjsvc. jar :\
/Usr/local/tomcat5/bin/commons-daemon.jar :\
Case "$1" in
Start)
#
# Start TlStat Data Serivce
#
$ DAEMON_HOME/jsvc \
-User $ MYJSVC_USER \
-Home $ JAVA_HOME \
-Djava. io. tmpdir = $ TMP_DIR \
-Wait 10 \
-Pidfile $ PID_FILE \
# The console output will be written to the tlstat. out file.
-Outfile $ MYJSVC_HOME/log/myjsvc. out \
-Errfile '& 1 '\
-Cp $ CLASSPATH \
# Service Startup
Com. sohu. jsvc. test. TestJsvc
#
# To get a verbose JVM
#-Verbose \
# To get a debug of jsvc.
#-Debug \
Exit $?
;;
5. Copy the myjsvc file to the/etc/init. d/directory.
6. # chmod-c 777/etc/init. d/myjsvc
7. Add a service
# Chkconfig -- add myjsvc
# Chkconfig -- level 345 myjsvc on
8. Complete. Start the service.
# Service myjsvc start
You can see the following information in the/test/log/myjsvc. out file:
Execute init (args) method
Execute start method
# Service myjsvc stop
You will find the following information added to the/test/log/myjsvc. out file:
Execute stop method
Execute destroy method
The myjsvc service is automatically started when the system restarts.
Well, a simple liunx service is ready. You can add your business in the init (), start (), stop (), destroy () Methods of TestJsvc, do what you want to do.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service