#需求: 把java -jar 的服务放到后台运行,并使用systemctl控制
#1,定义服务
[root@docker3 system]# cat /usr/lib/systemd/system/procedure.service
[Unit]
Description=my app-procedure
[Service]
ExecStart=/usr/bin/java -jar /root/myapp/procedure_jar/procedure.jar
[Install]
WantedBy=multi-user.target
#2,设置开机启动
[root@docker3 system]# systemctl enable procedure
Created symlink from /etc/systemd/system/multi-user.target.wants/procedure.service to /usr/lib/systemd/system/procedure.service.
#3,启动服务
[root@docker3 system]# systemctl start procedure
[root@docker3 system]# systemctl status procedure
● procedure.service - my app-procedure
Loaded: loaded (/usr/lib/systemd/system/procedure.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2020-04-22 06:57:07 UTC; 5s ago
Main PID: 2403 (java)
Tasks: 13
Memory: 110.2M
CGroup: /system.slice/procedure.service
└─2403 /usr/bin/java -jar /root/myapp/procedure_jar/procedure.jar
Apr 22 06:57:09 docker3 java[2403]: ' |____| .__|_| |_|_| |_\__, | / / / /
Apr 22 06:57:09 docker3 java[2403]: =========|_|==============|___/=/_/_/_/
Apr 22 06:57:09 docker3 java[2403]: :: Spring Boot :: (v2.1.4.RELEASE)
Apr 22 06:57:09 docker3 java[2403]: 2020-04-22 06:57:09.396 INFO 2403 --- [ main] com.proc.procedure.ProcedureApplication : Starting ProcedureAppli... root in /)
Apr 22 06:57:09 docker3 java[2403]: 2020-04-22 06:57:09.400 INFO 2403 --- [ main] com.proc.procedure.ProcedureApplication : No active profile set, ...es: default
Apr 22 06:57:12 docker3 java[2403]: 2020-04-22 06:57:12.582 INFO 2403 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with...8066 (http)
Apr 22 06:57:12 docker3 java[2403]: 2020-04-22 06:57:12.664 INFO 2403 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
Apr 22 06:57:12 docker3 java[2403]: 2020-04-22 06:57:12.665 INFO 2403 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine...cat/9.0.17]
Apr 22 06:57:12 docker3 java[2403]: 2020-04-22 06:57:12.852 INFO 2403 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring emb...tionContext
Apr 22 06:57:12 docker3 java[2403]: 2020-04-22 06:57:12.852 INFO 2403 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationCont... in 3306 ms
Hint: Some lines were ellipsized, use -l to show in full.
#4,停止服务
[root@docker3 system]# systemctl stop procedure
[root@docker3 system]# systemctl status procedure
● procedure.service - my app-procedure
Loaded: loaded (/usr/lib/systemd/system/procedure.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Wed 2020-04-22 06:57:19 UTC; 2s ago
Process: 2403 ExecStart=/usr/bin/java -jar /root/myapp/procedure_jar/procedure.jar (code=exited, status=143)
Main PID: 2403 (code=exited, status=143)
Apr 22 06:57:12 docker3 java[2403]: 2020-04-22 06:57:12.852 INFO 2403 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationCont... in 3306 ms
Apr 22 06:57:13 docker3 java[2403]: 2020-04-22 06:57:13.448 INFO 2403 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorSe...skExecutor'
Apr 22 06:57:13 docker3 java[2403]: 2020-04-22 06:57:13.971 INFO 2403 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(...ext path ''
Apr 22 06:57:13 docker3 java[2403]: 2020-04-22 06:57:13.980 INFO 2403 --- [ main] com.proc.procedure.ProcedureApplication : Started ProcedureApplic... for 6.107)
Apr 22 06:57:19 docker3 systemd[1]: Stopping my app-procedure...
Apr 22 06:57:19 docker3 java[2403]: 2020-04-22 06:57:19.787 INFO 2403 --- [ Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorS...skExecutor'
Apr 22 06:57:19 docker3 systemd[1]: procedure.service: main process exited, code=exited, status=143/n/a
Apr 22 06:57:19 docker3 systemd[1]: Stopped my app-procedure.
Apr 22 06:57:19 docker3 systemd[1]: Unit procedure.service entered failed state.
Apr 22 06:57:19 docker3 systemd[1]: procedure.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
Centos6服务器启动流程
[root@c6 init.d]# ll /etc/init.d/for
-rwxr-xr-x 1 root root 339 1月 17 11:45 /etc/init.d/for
[root@c6 init.d]# cat /etc/init.d/for
#!/bin/bash
#description: test service script
#chkconfig: 235 90 90
#prog: some cmd, but not stdout !
f(){
while :
do
echo `date` &>> /var/log/for.log
sleep 1
done
}
#调用系统脚本方法
. /etc/rc.d/init.d/functions
lock_file=/var/lock/subsys/for
pid_file=/var/run/for.pid
#服务管理:start,stop,status
case $1 in
stop)
killproc -p $pid_file
rm -f $lock_file $pid_file
;;
start)
f &
pid=$!
touch $lock_file
echo $pid > $pid_file
;;
status)
echo -n "service for: "
[ -f $lock_file -a -f $pid_file ] && success || failure
echo
esac
[root@c6 init.d]# chkconfig --add for
[root@c6 init.d]# chkconfig --list |grep for
for 0:off 1:off 2:on 3:on 4:off 5:on 6:off
[root@c6 init.d]# service for start [ OK ]
[root@c6 init.d]# service for status
service for: [ OK ]
[root@c6 init.d]# tailf /var/log/for.log
Fri Jan 17 12:11:18 CST 2020
Fri Jan 17 12:11:19 CST 2020
...
[root@c6 init.d]# service for stop [ OK ]
[root@docker3 procedure_jar]# cat /etc/init.d/procedure
#!/bin/bash
#description: start procedure.jar
#chkconfig: 235 90 90
#running os: CentOS 6.x
cmd=$1
port=8066
jar_file=/root/360_holo/procedure_jar/procedure.jar
log_file=/var/log/procedure.log
pidfile=/tmp/procedure.pid
. /etc/init.d/functions >/dev/null 2>&1
fun_start(){
java -jar $jar_file >$log_file 2>&1 &
echo $! > $pidfile
sleep 3
}
case $cmd in
start)
[ -f $pidfile ] && action "$jar_file already started !" /bin/true && exit
response=$(ss -nltp |grep $port)
[ $? -eq 0 ] && echo $response |kill `awk '{print $NF}' |awk -F',' '{print $2}' |awk -F'=' '{print $2}'` && sleep 2
fun_start &
[ $? -eq 0 ] && action "start $jar_file" /bin/true
;;
stop)
[ -f $pidfile ] && killproc -p $pidfile
res1=$?
response=$(ss -nltp |grep $port)
[ $? -eq 0 ] && echo $response |kill `awk '{print $NF}' |awk -F',' '{print $2}' |awk -F'=' '{print $2}'`
res2=$?
[ $res1 -eq 0 -o $res2 -eq 0 ] && action "stop $jar_file" /bin/true
;;
status)
[ -f $pidfile ] && action "$jar_file is " /bin/true
esac
[root@c6 ~]# chmod +x /etc/init.d/procedure
[root@c6 ~]# service procedure start
start /root/myapp/procedure_jar/procedure.jar [ OK ]
[root@c6 ~]# service procedure status
/root/myapp/procedure_jar/procedure.jar is [ OK ]
[root@c6 ~]# service procedure stop
stop /root/myapp/procedure_jar/procedure.jar [ OK ]
[root@docker3 procedure_jar]# chkconfig procedure on
[root@docker3 procedure_jar]# chkconfig |grep procedure
procedure 0:off 1:off 2:on 3:on 4:on 5:on 6:off
####1,服务脚本存放路径 ###
[root@c7 mysh]# ll /etc/init.d/
total 44
-rw-r--r--. 1 root root 18281 8月 24 2018 functions
lrwxrwxrwx. 1 root root 35 1月 11 00:27 jexec -> /usr/java/latest/.java/init.d/jexec
-rwxr-xr-x. 1 root root 1758 1月 11 06:57 mytomcat.sh
-rwxr-xr-x. 1 root root 4569 8月 24 2018 netconsole
-rwxr-xr-x. 1 root root 7923 8月 24 2018 network
-rw-r--r--. 1 root root 1160 10月 31 2018 README
####2,服务脚本内容 ###
[root@c7 mysh]# cat /etc/init.d/mytomcat.sh
#!/bin/bash
#description: start ctl script
#author: wang
#chkconfig: 345 90 90
tomcat_dir=/root/apache-tomcat-8.5.50
pidfile=/var/run/tomcat.pid
usage_invoke(){
cat <<EOF
error, Usage: $0 start|status|stop
EOF
exit 1
}
tomcat_invoke(){
cmd=$1
backend=$2
CATALINA_OUT=${tomcat_dir}/logs/catalina.out
touch $CATALINA_OUT
eval java '"-Djava.util.logging.config.file=${tomcat_dir}/conf/logging.properties"' \
' -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager' \
' -Djdk.tls.ephemeralDHKeySize=2048'\
' -Djava.protocol.handler.pkgs=org.apache.catalina.webresources '\
' -Dorg.apache.catalina.security.SecurityListener.UMASK=0027' \
-classpath '"${tomcat_dir}/bin/bootstrap.jar:${tomcat_dir}/bin/tomcat-juli.jar"'\
' -Dcatalina.base="${tomcat_dir}"'\
' -Dcatalina.home="${tomcat_dir}"' \
' -Djava.io.tmpdir="${tomcat_dir}/temp"' \
org.apache.catalina.startup.Bootstrap $cmd >> $CATALINA_OUT 2>&1 "$backend"
}
start_invoke(){
status_invoke
if [ $? -eq 1 ] ;then
tomcat_invoke start "&"
pid=$!
echo $pid >$pidfile
echo "server start at pid: $pid "
fi
}
stop_invoke(){
status_invoke
[ $? -eq 0 ] && tomcat_invoke stop && rm -f $pidfile
}
status_invoke(){
if [ -e $pidfile ] ;then
if ps -ef |awk '{print $2}' |grep `cat $pidfile` ; then
return 0;
else
return 1;
fi
else
return 1;
fi
}
#1,require action args
[ $# -eq 0 ] && usage_invoke
#2,decide how to invoke the method by the action args
args=$1
case $args in
start)
start_invoke
;;
stop)
stop_invoke
;;
status)
status_invoke
[ $? -eq 0 ] && echo "tomcat already started !" || echo "tomcat not started !"
;;
*)
usage_invoke
esac
####1, 服务开机启动 ###
[root@c7 mysh]# chkconfig --add mytomcat.sh
[root@c7 mysh]# chkconfig --list
jexec 0:off 1:on 2:on 3:on 4:on 5:on 6:off
mytomcat.sh 0:off 1:off 2:off 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
####2,测试服务调用 ###
[root@c7 ~]# service mytomcat.sh start
server start at pid: 14722
[root@c7 ~]# service mytomcat.sh status
14722
tomcat already started !
[root@c7 ~]# ss -nltp |grep 8080
LISTEN 0 100 :::8080 :::* users:(("java",pid=14722,fd=40))
[root@c7 ~]# service mytomcat.sh stop
14722
[root@c7 ~]# ss -nltp |grep 8080
[root@c7 ~]#
####3,查看日志文件 ###
[root@c7 mysh]# service mytomcat.sh start
server start at pid: 17986
[root@c7 mysh]# date
2020年 01月 11日 星期六 10:02:44 CST
[root@c7 mysh]# tailf /root/apache-tomcat-8.5.50/logs/catalina.out
11-Jan-2020 10:02:24.776 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/root/apache-tomcat-8.5.50/webapps/docs] has finished in [59] ms
11-Jan-2020 10:02:24.777 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/root/apache-tomcat-8.5.50/webapps/examples]
cat >/etc/init.d/cdh-server <<EOF
#!/bin/bash
# chkconfig: 2345 90 90
# description: cdh.service ....
agent=/opt/cm-5.*/etc/init.d/cloudera-scm-agent
server=/opt/cm-5.*/etc/init.d/cloudera-scm-server
cmd=\$1
case \$1 in
start) \$server start; \$agent start ;;
stop) \$agent stop; \$server stop ;;
status) \$server status; \$agent status;;
restart) \$server restart; \$agent restart;;
* ) echo "Usage : \$server start|restart|stop|status"
esac
EOF
chmod +x /etc/init.d/cdh-server
chkconfig cdh-server on