search

Linux service management

Jun 23, 2017 pm 01:50 PM
Servemanage


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>  # 将指定名称的服务在指定级别上打开开机自启动或关闭开机自启动功能。
                                                    # reset则表示重置为脚本中指定的级别</on></name></levels></name>

当然,除了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
Linux's Essential Components: Explained for BeginnersLinux's Essential Components: Explained for BeginnersApr 17, 2025 am 12:08 AM

The core components of Linux include the kernel, file system, shell and common tools. 1. The kernel manages hardware resources and provides basic services. 2. The file system organizes and stores data. 3. Shell is the interface for users to interact with the system. 4. Common tools help complete daily tasks.

Linux: A Look at Its Fundamental StructureLinux: A Look at Its Fundamental StructureApr 16, 2025 am 12:01 AM

The basic structure of Linux includes the kernel, file system, and shell. 1) Kernel management hardware resources and use uname-r to view the version. 2) The EXT4 file system supports large files and logs and is created using mkfs.ext4. 3) Shell provides command line interaction such as Bash, and lists files using ls-l.

Linux Operations: System Administration and MaintenanceLinux Operations: System Administration and MaintenanceApr 15, 2025 am 12:10 AM

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.

Understanding Linux's Maintenance Mode: The EssentialsUnderstanding Linux's Maintenance Mode: The EssentialsApr 14, 2025 am 12:04 AM

Linux maintenance mode is entered by adding init=/bin/bash or single parameters at startup. 1. Enter maintenance mode: Edit the GRUB menu and add startup parameters. 2. Remount the file system to read and write mode: mount-oremount,rw/. 3. Repair the file system: Use the fsck command, such as fsck/dev/sda1. 4. Back up the data and operate with caution to avoid data loss.

How Debian improves Hadoop data processing speedHow Debian improves Hadoop data processing speedApr 13, 2025 am 11:54 AM

This article discusses how to improve Hadoop data processing efficiency on Debian systems. Optimization strategies cover hardware upgrades, operating system parameter adjustments, Hadoop configuration modifications, and the use of efficient algorithms and tools. 1. Hardware resource strengthening ensures that all nodes have consistent hardware configurations, especially paying attention to CPU, memory and network equipment performance. Choosing high-performance hardware components is essential to improve overall processing speed. 2. Operating system tunes file descriptors and network connections: Modify the /etc/security/limits.conf file to increase the upper limit of file descriptors and network connections allowed to be opened at the same time by the system. JVM parameter adjustment: Adjust in hadoop-env.sh file

How to learn Debian syslogHow to learn Debian syslogApr 13, 2025 am 11:51 AM

This guide will guide you to learn how to use Syslog in Debian systems. Syslog is a key service in Linux systems for logging system and application log messages. It helps administrators monitor and analyze system activity to quickly identify and resolve problems. 1. Basic knowledge of Syslog The core functions of Syslog include: centrally collecting and managing log messages; supporting multiple log output formats and target locations (such as files or networks); providing real-time log viewing and filtering functions. 2. Install and configure Syslog (using Rsyslog) The Debian system uses Rsyslog by default. You can install it with the following command: sudoaptupdatesud

How to choose Hadoop version in DebianHow to choose Hadoop version in DebianApr 13, 2025 am 11:48 AM

When choosing a Hadoop version suitable for Debian system, the following key factors need to be considered: 1. Stability and long-term support: For users who pursue stability and security, it is recommended to choose a Debian stable version, such as Debian11 (Bullseye). This version has been fully tested and has a support cycle of up to five years, which can ensure the stable operation of the system. 2. Package update speed: If you need to use the latest Hadoop features and features, you can consider Debian's unstable version (Sid). However, it should be noted that unstable versions may have compatibility issues and stability risks. 3. Community support and resources: Debian has huge community support, which can provide rich documentation and

TigerVNC share file method on DebianTigerVNC share file method on DebianApr 13, 2025 am 11:45 AM

This article describes how to use TigerVNC to share files on Debian systems. You need to install the TigerVNC server first and then configure it. 1. Install the TigerVNC server and open the terminal. Update the software package list: sudoaptupdate to install TigerVNC server: sudoaptinstalltigervnc-standalone-servertigervnc-common 2. Configure TigerVNC server to set VNC server password: vncpasswd Start VNC server: vncserver:1-localhostno

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)