...
在 CentOS7 上將自定義的 jar 包註冊為 linux 服務 service
1、在 /etc/rc.d/init.d/ 目錄下創建一個名字和服務名完全相同的 shell 腳本文件 joyupx:
vim joyupx
/etc/rc.d/init.d/ 目錄中存放的一般都是 shell 腳本文件,但是文件名不可以帶尾碼“.sh”;
服務名必須與其 /etc/rc.d/init.d/ 目錄下關聯的配置文件名稱完全相同,否則報找不到這樣的服務;
2、在 joyupx 文件中加入如下代碼:
#!/bin/bash
# 級別
#chkconfig: 2345 80 90
# 描述
#description:joyupx service
# 進程名
#processname:joyupx-service
# 我們的 jar 包所在的目錄
ROOT_PATH=/usr/local/services/tomcat/apache-tomcat-9.0.0.M9/webapps
# JDK 的根目錄
JAVA_HOME=/usr/local/services/jdk/jdk1.8.0_91
PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
# CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
# CLASSES=${CLASSPATH}:/usr/work/space1/bin
# export JAVA_HOME
# export PATH
# export CLASSPATH
# source function library
#. /etc/rc.d/init.d/functions
start()
{
echo -n $"Starting $prog: "
# java -jar /usr/local/services/tomcat/apache-tomcat-9.0.0.M9/webapps/joyupx-service-jar-with-dependencies.jar
java -jar $ROOT_PATH/joyupx-service-jar-with-dependencies.jar
# java -jar *.jar &
echo "joyupx service"
# java -version
# echo "CLASSPATH: ${CLASSPATH}"
# echo "PATH: ${PATH}"
# java -classpath $CLASSES MyFrame
# /usr/work/space1/bin/MyFrame start
}
stop()
{
echo -n $"Stopping $prog: "
javac -version
# java -jar /usr/local/services/tomcat/apache-tomcat-9.0.0.M9/webapps/joyupx-service-jar-with-dependencies.jar
}
restart()
{
echo -n $"Reloading $prog: "
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart}"
RETVAL=1
esac
exit $RETVAL
保存並退出:
ESC
:wq!
3、為 joyupx 賦可執行的許可權:
chmod a+x joyupx
4、啟動 joyupx 服務:
service joyupx start
5、查看 joyupx 的進程:
ps -aux | grep joyupx