1.例子:如何给java服务实现自启动。解决机器重启后保持服务正常。
获取java服务的路径
image.png
2.编写启动脚本
这里可以写代码或者文字
#!/bin/sh
# chkconfig: 2345 90 90
# Description: {{ app }} service
. /etc/init.d/functions
export HOME
source /etc/profile
PATH=$PATH:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
HYBRID_APP_HOME=/usr/local/services/{{ app }}/
cd ${HYBRID_APP_HOME}
function start() {
/bin/bash ${HYBRID_APP_HOME}/bin/startup.sh
if [ $? -eq 0 ];then
action "Starting {{ app }} Server" /bin/true
else
action "Starting {{ app }} Server" /bin/false
fi
PID=`ps -ef | grep '{{ app }}' | egrep -v 'grep' | awk '{print $2}'`
echo $PID > /var/run/{{ app }}.pid
}
function stop() {
/bin/bash ${HYBRID_APP_HOME}/bin/shutdown.sh
if [ $? -eq 0 ];then
action "Starting {{ app }} Server" /bin/true
else
action "Starting {{ app }} Server" /bin/false
fi
}
function status() {
PID=`ps -ef | grep '{{ app }}' | egrep -v 'grep' | awk '{print $2}'`
if [ $PID ];then
echo -e "{{ app }} \033[32mRunning\033[0m (PID:$PID)..."
else
echo -e "{{ app }}\033[31m Not running\033[0m..."
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
$0 stop
$0 start
;;
status)
status
;;
*)
echo "Usage: service {{ app }} {start|stop|restart|status}"
exit 1
esac
exit 0
3.把相应的路径写上。放到/etc/ini.d下
image.png
4.执行
chkconfig --add cas
/sbin/chkconfig cas on
即可