System environment:
CentOS 7 X64
JDK1.8
One:
Installing JSVC
Download the Commons-daemon source package
Http://apache.fayea.com//commons/daemon/source/commons-daemon-1.0.15-src.zip
Unzip after CD src/native/unix/
Run:
Export cflags=-m64
Export ldflags=-m64
Note: These 2 code only have 64-bit system to need
./configure
Make
After that, the Jsvc file is generated under the current folder and copied to a folder, here is the/home/clouder/vs/jsvc folder
Then, add the Jsvc_home to the environment variable
Vim. BASHRC
Export JSVC_HOME=/HOME/CLOUDER/VS/JSVC
Two: Write Java code
The start class must implement the Daemon interface, and then override Start (), and the Stop () method can
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: The pom.xml inside needs to be added such as 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 folder 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