System environment:
CentOS 7 X64
JDK1.8
One:
Installing JSVC
Download Commons-daemon's Source package
Http://apache.fayea.com//commons/daemon/source/commons-daemon-1.0.15-src.zip
Unzip after CD src/native/unix/
Perform:
Export cflags=-m64
Export ldflags=-m64
Note: This 2-sentence code is only required for 64-bit systems
./configure
Make
Once completed, the Jsvc file will be generated in the current directory, copied to a directory, this is the/HOME/CLOUDER/VS/JSVC directory
Then, add the Jsvc_home to the environment variable
Vim. BASHRC
Export JSVC_HOME=/HOME/CLOUDER/VS/JSVC
Two: Write Java code
The startup class must implement the Daemon interface, and then override the start () and the Stop () method to
Package Com.lala;import Org.apache.commons.daemon.daemon;import Org.apache.commons.daemon.daemoncontext;import Org.apache.commons.daemon.daemoninitexception;public class Application implements Daemon{myserver server = null; public void init (Daemoncontext context) throws Daemoninitexception,exception {server = new MyServer ();} public void Start () throws Exception {Server.start ();} public void Stop () throws Exception {Server.stop ();} public void Destroy () {system.exit (0);}}
Note: Pom.xml must include the following dependencies:
<dependency><groupid>commons-daemon</groupid><artifactid>commons-daemon</artifactid ><version>1.0.15</version></dependency>
Three: Writing shell scripts
server.sh
#!/bin/bashif ["$JAVA _home" = ""]; Then echo "Error:java_home are not set." Exit 1fiif ["$JSVC _home" = ""]; Then echo "Error:jsvc_home are not set." Exit 1fibin= ' DirName "$" ' Export myjetty_home= ' CD $bin/... /; PWD ' myjetty_conf_dir= $MYJETTY _home/confmyjetty_work_dir= $MYJETTY _home/workclasspath= "${myjetty_conf_dir}" for F In $MYJETTY _home/lib/*.jar; Do Classpath=${classpath}: The directory where the $f;d onelog_dir=${myjetty_home}/logsclass=com.lala.application#jsvc Daemon_home= $JSVC _homepid=${myjetty_work_dir}/myjetty.pid Case "$" in start) $DAEMON _home/jsvc- djava.io.tmpdir= $MYJETTY _work_dir-wait 5000-pidfile $PID-outfile log_dir/myjetty.out-errfile ' &1 '-CP $CLASSPATH $CLASS exit $? ;; Stop) $DAEMON _home/jsvc-stop-pidfile $PID $CLASS exit $? ; *) echo "Usage jsvc start,stop" exit 1;; Esac
Finally, use
SH server.sh start Service
SH server.sh stop shutdown Service
Start and close Java programs using Commons-daemon