前言
本地环境:CentOS Linux release 7.4.1708 (Core)
配置路径:/etc/init.d/
系统服务:chkconfig
1.1、redis服务脚本
在下载redis的gz包里,有utils目录,里面有redis_init_script脚本,类似下载的配置文件;也可以在/etc/init.d/目录里创建以下的脚本
vim /etc/init.d/redis
+++/etc/init.d/redis配置+++++++++++++++
#!/bin/bash
#chkconfig: 2345 10 90
#description: Start and Stop redis
REDISPORT=6379
EXEC=/usr/local/redis-4.0.10/src/redis-server
CLIEXEC=/usr/local/redis-4.0.10/src/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/usr/local/redis-4.0.10/redis.conf"
case "$1" in
start)
if [ -f $PIDFILE ];then
echo "$PIDFILE exists,process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ];then
echo "$PIDFILE does not exist,process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart)
"$0" stop
sleep 3
"$0" start
;;
*)
echo "Please use start or stop or restart as first argument"
;;
esac
+++/etc/init.d/redis配置文件+++++++++++++++
注释:
#chkconfig: 2345 10 90
redis服务必须在运行级2,3,4,5下被启动或关闭,10是启动优先级,90是停止优先级,优先级范围是0-100,数字越大,优先级越低。
1.2、在chkconfig添加redis服务管理
chmod +x /etc/init.d/redis
chkconfig --add redis
chkconfig redis on
service redis {start | stop | restart}
+++华丽的分隔线+++++++++++++++++++++
2.1创建tomcat服务脚本
vim /etc/init.d/tomcat
+++++/etc/init.d/tomcat配置++++++++
#!/bin/bash
# description: Tomcat7 Start Stop Restart
# processname: tomcat7
# chkconfig: 234 20 80
CATALINA_HOME=/usr/local/tomcat/
case $1 in
start)
sh $CATALINA_HOME/bin/startup.sh
;;
stop)
sh $CATALINA_HOME/bin/shutdown.sh
;;
restart)
sh $CATALINA_HOME/bin/shutdown.sh
sh $CATALINA_HOME/bin/startup.sh
;;
*)
echo 'please use : tomcat {start | stop | restart}'
;;
esac
+++++/etc/init.d/tomcat配置++++++++
2.2、在chkconfig添加tomcat服务管理
chmod +x /etc/init.d/tomcat
chkconfig --add tomcat
chkconfig tomcat on
service tomcat {start | stop | restart}