Home  >  Article  >  Operation and Maintenance  >  Linux service management

Linux service management

巴扎黑
巴扎黑Original
2017-06-23 13:50:392085browse

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 7

CentOS 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

Service is a process that provides services to the outside world. Generally speaking, it will be placed in the background. Since it needs to continuously provide external services at any time For service requests sent, the service process needs to be resident in the memory and should not be related to the terminal. Otherwise, the terminal will exit the service program when it exits. In addition, to be able to receive external requests and provide services to the outside world, you need to have a "service window" dedicated to this service. This service window is the port number, and the service provider can be found through the port number.

The end that provides services is called the server, and the one that requests services from the server is called the client. First, the server starts the service process, and the corresponding port number will be opened at this time; then the client specifies the server IP address and port number to initiate a request to the server. The kernel of the host where the server is located receives the request packet and then analyzes the data. The packet discovery request is for a certain port number. The kernel knows which application is listening to the port number, so it sends the request message to the corresponding application. After the application receives the message, it will establish a connection with the client. and perform data transmission.

It should also be noted that not all services always provide port numbers. For example, the xinetd service only takes over the corresponding port when needed. For example, when the rsync listening port is 222, then request rsync At this time, the port number of xinetd during the listening process is 222. When not requested, xinetd has no port number.

In Linux, services are divided into independent daemons and super daemons. The independent daemon process monitors itself in the background. Basically all services are independent daemon class services. The super daemon specifically refers to the service xinetd. This service manages some special services on its behalf. When such services are requested, xinetd will notify it to start the service. After the service is provided, the service will be closed. This type of service is called transient. Daemon processes exist only temporarily.

But you must understand that the super daemon xinetd itself is an independent daemon process that resides in memory, because it has to listen to requests from the outside world for the transient daemon processes it manages. However, when it is not working, xinetd does not occupy the port number. When it is working, it occupies the port number of the requested transient daemon and is in a listening state.

11.2 Managing independent daemons

On CentOS 6, all service scripts are in the /etc/rc.d/init.d/ directory , /etc/init.d/ is its soft link. The scripts in this directory are all LSB style scripts, and they basically accept parameters such as start/stop/restart/reload/status.

[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
To manage independent daemon class services

/etc/init.d/service_name   restart|start|stop|status    # 方法一
service  service_name    restart|start|stop|status    # 方法二
To allow the service to It is managed by the service command, and its service script can be placed in the /etc/init.d directory.

11.3 Management service starts automatically at boot

The chkconfig command can manage the scripts that exist in the /etc/init.d/ directory and the content of the script meets certain conditions Serve.

To enable chkconfig to manage whether the service starts automatically at boot, just place the script in the /etc/init.d directory, and then add the chkconfig line and description line in front of the script. For example:

#!/bin/bash

# chkconfig: - 85 15# description: The Apache HTTP Server is an efficient and extensible

These 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文件中也是可以的。


11.4 管理xinetd及相关瞬时守护进程

11.4.1 管理瞬时守护进程

该类服务不能直接使用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

11.4.2 瞬时守护进程的配置

瞬时守护进程受两个配置文件控制,一个是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个选项的意义如下图。


11.5 CentOS 7上管理服务

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn