For project development on Linux, a common method for Java project deployment is to write a shell script. However, when the server restarts, you often forget to start the shell script.
Therefore, we need to turn our applications into Linux services and start our own applications when the server starts. Jsvc can be used to implement the above functions.
For details about jsvc, see: http://commons.apache.org/daemon/jsvc.html
1. Install jsvc
Download the jsvc.tar.gz File
# 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 test;
Public class testjsvc {
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! ");
}
}
Among them, 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.
4. write the script myjsvc to start the service, where the red part should be modified according to the actual situation.
#! /Bin/sh
# Myjsvc this shell script takes care of starting and stopping
#
# Chkconfig:-60 50
# Description: test a daemon.
# Processname: myjsvc
# Source function library.
./Etc/rc. d/init. d/functions
Retval = 0
Prog = "myjsvc"
# JDK installation directory
Java_home =/usr/Java/jdk1.5.0 _ 15
# Application directory
Myjsvc_home =/test
# Jsvc directory
Daemon_home =/usr/local/tomcat5/bin/jsvc-Src
# User
Myjsvc_user = root
# 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 Serivce
#
$ Daemon_home/jsvc \
-User $ myjsvc_user \
-Home $ java_home \
-Djava. Io. tmpdir = $ tmp_dir \
-Wait 10 \
-Pidfile $ pid_file \
-OUTFILE $ myjsvc_home/log/myjsvc. Out \
-Errfile '& 1 '\
-CP $ classpath \
Test. testjsvc
# To get a verbose JVM
#-Verbose \
# To get a debug of jsvc.
#-Debug \
Exit $?
;;
Stop)
#
# Stop Serivce
#
$ Daemon_home/jsvc \
-Stop \
-Pidfile $ pid_file \
Test. testjsvc
Exit $?
;;
*)
Echo "usage myjsvc start/stop"
Exit 1 ;;
Esac
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 sets the Service Startup level
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.