在 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