您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
马哥linux运维学习笔记-bash脚本编程之十一(Linux启动流程之三) SysV服务脚本
发布时间:2018-09-23 12:17:43编辑:雪饮阅读()
sysV服务脚本
sysV风格服务脚本都遵循以下命令风格:
start|stop|restart|status
reload|configtest
sysV风格服务脚本位于/etc/rc.d/init.d中
观察sysV风格服务脚本
进入/etc/rc.d/init.d目录中随便找两个脚本用head查看下,如
[root@localhost init.d]# head netconsole network
==> netconsole <==
#!/bin/bash
#
# netconsole This loads the netconsole module with the configured parameters.
#
# chkconfig: - 50 50
# description: Initializes network console logging
# config: /etc/sysconfig/netconsole
#
# Copyright 2002 Red Hat, Inc.
#
==> network <==
#! /bin/bash
#
# network Bring up/down networking
#
# chkconfig: 2345 10 90
# description: Activates/Deactivates all network interfaces configured to \
# start at boot time.
#
### BEGIN INIT INFO
# Provides: $network
[root@localhost init.d]#
经过观察会发现,它们都有共同段chkconfig和description
sysV风格脚本中的chkconfig和description
chkconfig:该段一般都是3组数值,分别是启动级别、启动顺序、关闭顺序
description:该脚本的一些功能描述,如果需要换行则使用"\"
sysV风格服务脚本的定义与sysV服务的管理
sysV风格服务脚本必须定义在/etc/rc.d/init.d中且无后缀的文件
进入该目录定义一个脚本如:
vi snowDrink
#!/bin/bash
#
# chkconfig: 2345 77 22
# description: Test Service
#
LOCKFILE=/var/lock/subsys/myservice
status() {
if [ -e $LOCKFILE ]; then
echo "Running..."
else
echo "Stopped"
fi
}
usage() {
echo "`basename $0` {start|stop|restart|status}"
}
case $1 in
start)
echo "Starting..."
touch $LOCKFILE;;
stop)
echo "Stopping..."
rm -f $LOCKFILE &> /dev/null
;;
restart)
echo "Restarting...";;
status)
status ;;
*)
usage ;;
esac
给该脚本授权
chmod +x snowDrink
添加到sysv服务
chkconfig --add snowDrink
查看sysv服务列表
[root@localhost init.d]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
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
snowDrink 0:off 1:off 2:on 3:on 4:on 5:on 6:off
删除指定服务
chkconfig --del snowDrink
查看指定服务
[root@localhost init.d]# chkconfig --list snowDrink
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
snowDrink 0:off 1:off 2:on 3:on 4:on 5:on 6:off
设定指定级别下服务的启动或关闭
chkconfig --level 24 snowDrink off
xinetd
xinetd是超级守护进程
xinetd命令可能系统中默认没有,可以通过yum安装
yum install xinetd
安装完成后启动服务
service xinetd start
然后使用chkconfig --list会发现多出来如下信息:
xinetd based services:
chargen-dgram: off
chargen-stream: off
daytime-dgram: off
daytime-stream: off
discard-dgram: off
discard-stream: off
echo-dgram: off
echo-stream: off
tcpmux-server: off
time-dgram: off
time-stream: off
多出的这一组服务都是通过xinetd来管理的,而被xinetd管理的每个服务称为瞬时进程
会发现瞬时进程没有级别的设定,只有关闭与开启,而我们sysv服务都是有级别的设定的,其实sysV服务被chkconifg管理时--level是可选的,若没有该项则默认级别是2345级别
瞬时进程管理
有了xinetd后就有了瞬时进程
瞬时进程管理语法:chkconfig 进程名 on/off
瞬时进程的开启
chkconfig chargen-dgram on
瞬时进程的关闭
chkconfig chargen-dgram off
关键字词:inux,sysV,bash