如果软件安装后,使用service xxx start 显示unrecognized service,服务无法被识别,或者需要设置开机启动,避免机器重启后软件服务没有启动,可以进行如下设置.
下面以nginx服务为例
1.设置服务名称识别
要点解释:
/etc/init.d/和/etc/rc.d/init.d目录是同一目录,实际操作的目录就是/etc/rc.d/init.d
该目录下的文件能直接使用service 文件名 识别,放到这个路径下的文件 相当于注册了系统的服务.
#/etc/init.d -> /etc/rc.d/init.d 即 /etc/init.d 通过设置软连接的方式指向/etc/rc.d/init.d
1)实例一:在/etc/init.d目录下添加nginx脚本,脚本名称取名为nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
# 这里要根据实际情况修改,实际编译安装目录
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
# 这里要根据实际情况修改
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
killall -9 nginx
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
1-2)实例二
#!/bin/bash
. /etc/profile
. ~/.bash_profile
#name="run_badbatt.sh"
start(){
echo "start service"
nohup /app/jdk1.8.0_202/bin/java -jar /app/badbatt.jar &
echo -n "started"
}
stop(){
echo "stop service"
pid=$(ps -ef | grep batt_ms_x64_fbsdev.jar | grep -v grep | awk '{print $2}')
if [ -n "$pid" ]; then
kill -9 $pid;
fi
}
restart(){
stop
sleep 1
start
}
case "$1" in
start)
$1
;;
stop)
$1
;;
restart)
$1
;;
esac
2)对脚本添加执行权限
chmod +x /etc/init.d/nginx
2.设置开机启动(两种常用方式,以nginx为例)
要点解释:
#方式1-修改rc.local文件
/etc/rc.local #是个可执行的脚本文件,里面添加指令,机器启动时可以指令
机器启动时,会加载执行rc.local这个脚本
#方式2-chkconfig命令
chkconfig 主要用来更新(启动或停止)和查询系统服务的运行级信息
使用范例:
chkconfig --list #列出所有的系统服务
chkconfig --add httpd #增加httpd服务
chkconfig --del httpd #删除httpd服务
chkconfig --level httpd 2345 on #设置httpd在运行级别为2、3、4、5的情况下都是on(开启)的状态
chkconfig --list #列出系统所有的服务启动情况
chkconfig --list nginx #列出nginx服务设置情况
chkconfig --level 35 nginx on #设定nginx在等级3和5为开机运行服务
#level 35表示操作只在等级3和5执行,on表示启动,off表示关闭
chkconfig nginx on #设定nginx在各等级为on,“各等级”包括2、3、4、5等级
1)方式1-修改/etc/rc.local文件
/etc/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
...
touch /var/lock/subsys/local
service nginx start #启动nginx服务
...
2)方式2-chkconfig指令
#使用chkconfig指令
重点说明:服务脚本必须存放在/etc/init.d/目录下,不然chkconfig指令添加服务名称会无法识别
#1.服务添加到/etc/init.d同级的rcN.d的入口
chkconfig --add nginx #此时,因为还没设置服务的level,暂不会下方到某个rcN.d目录,但是在入口
#2.确认服务的默认启动等级。
chkconfig --level 2345 nginx on#设置了level为2345后,可以在init.d同级的rc2.d/rc3.d/rc4.d/rc5.d目录下看到nginx
#机器启动时,会按照机器的启动级别加载rcN.d中的服务