Home >Operation and Maintenance >Linux Operation and Maintenance >Linux service management
Directory of this article:
##11.1 Concept of Service
11.2 Managing independent daemons
##11.3 Managing the self-starting of services
11.4 Managing xinetd and related transient daemons
##11.5 Managing services on CentOS 7CentOS 7 and CentOS 6 manage services completely differently. This article first explains the management methods on CentOS 6, and finally lists the service management methods on CentOS 7.
11.1 The concept of service
11.2 Managing independent daemons
[root@xuexi tmp]# ls /etc/init.d abrt-ccpp cpuspeed irqbalance messagebus psacct saslauthd abrtd crond kdump netconsole quota_nld single abrt-oops functions killall netfs rdisc smartd acpid haldaemon lvm2-lvmetad network restorecond sshd atd halt lvm2-monitor ntpd rngd svnserve auditd ip6tables mcelogd ntpdate rsyslog sysstat blk-availability iptables mdmonitor postfix sandbox udev-post
/etc/init.d/service_name restart|start|stop|status # 方法一 service service_name restart|start|stop|status # 方法二
11.3 Management service starts automatically at boot
#!/bin/bash # chkconfig: - 85 15# description: The Apache HTTP Server is an efficient and extensibleThese two lines must be in front of all non-comment lines, and these two lines must be "commented". The "-" in the chkconfig line indicates that it is applicable to run level 123456, 85 indicates that when it is turned on, its startup sequence is 85, and 15 indicates that when it is shut down and the service is stopped, its stop sequence is 15. You can just give some description information in the description line, but you must give the "description:" keyword. Then, you can use chkconfig to manage the self-starting of the service.
chkconfig [--add | --del] <name> # 将/etc/init.d中可以被chkconfig管理的服务添加到chkconfig的管理列表中,或者从列表中删除 chkconfig [--list] [name] # 列出指定名称的服务的开启自启动信息。name可以使用all来表示列出所有chkconfig管理列表中的服务 chkconfig [--level <levels>] <name> <on|off|reset> # 将指定名称的服务在指定级别上打开开机自启动或关闭开机自启动功能。 # reset则表示重置为脚本中指定的级别
当然,除了chkconfig可以管理开机自启动,将启动命令放在/etc/rc.d/rc.local文件中也是可以的。
该类服务不能直接使用service命令来启动。只能去/etc/xinetd.d/目录下的对应文件中进行设置(当然,也可以在/etc/xinetd.conf中配置),然后由xinetd进行管理。
首先安装xinetd程序。
[root@xuexi tmp]# yum -y install xinetd [root@xuexi tmp]# chkconfig --list ......省略 xinetd 0:off 1:off 2:off 3:on 4:on 5:on 6:off xinetd based services: chargen-dgram: off chargen-stream: off daytime-dgram: off daytime-stream: off discard-dgram: off discard-stream: offecho-dgram: offecho-stream: off rsync: off tcpmux-server: offtime-dgram: offtime-stream: off
首先得保证xinetd是已经工作在后台的。
service xinetd start
然后管理瞬时守护进程,该类服务比较特别,其自启动状态和服务运行状态是同步的,也就是说chkconfig设置了其自启动则表示启动该服务,否则为停止该服务。另外,对其指定级别是无效的,它们的启动级别继承与xinetd的启动级别,并且xinetd会接管其触发的瞬时守护进程的端口号。
例如启动rsync这个瞬时守护进程。
chkconfig rsync on
瞬时守护进程受两个配置文件控制,一个是xinetd的配置文件/etc/xinetd.conf提供默认配置,一个是/etc/xinetd.d/下的配置文件针对对应的服务提供配置。
例如配置rsync,以下是/etc/xinetd.d/rsync的默认配置。
[root@xuexi tmp]# vi /etc/xinetd.d/rsync # default: off # description: The rsync server is a good addition to an ftp server, as it \ # allows crc checksumming etc. service rsync # 定义rsync服务,名称要和/etc/xinetd.d/下的文件同名 { disable = yes # yes表示不启动,no表示启动,等价于chkconfig rsync {on|off},所以这里设置后将直接在chkconfig中生效 flags = IPv6 # 不用管 socket_type = stream # 这代表的是tcp类型的套接字wait = no # 该服务是单线程还是多线程的,表现形式是超出的请求是否进行等待,no表示多线程 user = root # 以什么身份运行rsync server = /usr/bin/rsync # 服务程序 server_args = --daemon # 服务程序启动时传递的参数 log_on_failure += USERID # 连接失败的日志记录,+表示在全局对应的条目上新增此处指定的USERID }
除此之外,还有几个选项:
【访问控制选项】以下两个控制列表中最好不要出现冲突的地址。 only_from:定义允许连接的访问控制列表,支持单IP,CIDR格式和长mask格式的网段,主机名hostname,域DOMAIN(.abc.com) no_access:定义不允许访问的列表,语法格式同only_from 【监听地址】 bind = ip_addr interface = ip_addr # 等价于bind 【资源控制】 cps=args1 args2 instances=N per_source=N
这3个选项的意义如下图。
service name start ==> systemctl start name.service
service name stop ==> systemctl stop name.service
service name restart ==> systemctl restart name.service
service name status ==> systemctl status name.service
查看服务是否激活(在运行中):systemctl is-active name.service
查看所有已经激活 :systemctl list-units --type service
查看所有服务 :systemctl list-units --type service --all
设置开机自启动:chkconfig name on ==> systemctl enable name.service
禁止开机自启动:chkconfig name off ==> systemctl disable name.service
查看服务是否开机自启动:chkconfig --list name ==> is-enabled name.service
查看所有服务的开机自启动状态:chkconfig --list ==> systemctl list-unit-files --type service
回到系列文章大纲:
The above is the detailed content of Linux service management. For more information, please follow other related articles on the PHP Chinese website!