linux 下的 service 和systemctl 服务管理方式

linux 下的 service  和systemctl 服务管理方式

man service 得到:执行一个system V 风格的启动脚本

service - run a System V init script

man systemctl 得到: 控制systemd 系统和服务管理器

systemctl - Control the systemd system and service manager

 相关连接机官方文档:

radhat 关于systemd 官方文档:https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/pdf/system_administrators_guide/Red_Hat_Enterprise_Linux-7-System_Administrators_Guide-en-US.pdf

手册说明 有些官方找不到的参数这里查 :https://www.freedesktop.org/software/systemd/man/systemd.exec.html

  • systemd:完全替代init,可并行启动服务,并能减少在shell上的系统开销,相比传统的System V是一大革新,已被大多数Linux发行版所使用。

  init(为英语:initialization的简写)是 Unix 和 类Unix 系统中用来产生其它所有进程的程序。它以守护进程的方式存在,其进程号为1。Linux系统在引导时加载Linux内核后,便由Linux内核加载init程序,由init程序完成余下的引导过程,比如加载运行级别,加载服务,引导Shell/图形化界面等等。

  Unix 系列中(如 System III 和 System V)init的作用,和研究中的 Unix 和 BSD 派生版本相比,发生了一些变化。大多数Linux发行版是和 System V 相兼容的,但是一些发行版如Slackware 采用的是BSD风格,其它的如 Gentoo 是自己定制的。后来Ubuntu[1][2] 和其他一些发行版采用 Upstart[3] 来代替[4] 传统的 init 进程。至2015年,大部分Linux发行版都已采用新的systemd替代System V和Upstart,但systemd向下兼容System V。

1.service

执行 service 命令就是在 /etc/init.d/ 执行可执行的sh 脚本,start|stop|restart 等命令参数都是脚本中定义好的函数

root@localhost init.d]# pwd
/etc/init.d
[root@localhost init.d]# ls
functions  netconsole  network  README
[root@localhost init.d]# cat 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
# Should-Start: iptables ip6tables NetworkManager-wait-online NetworkManager $network-pre
# Short-Description: Bring up/down networking
# Description: Bring up/down networking
### END INIT INFO

# Source function library.
. /etc/init.d/functions

  

2.systemctl

systemd是Linux系统最新的初始化系统(init),作用是提高系统的启动速度,尽可能启动较少的进程,尽可能更多进程并发启动。
systemd对应的进程管理命令是systemctl, systemctl 也兼容了 service 功能

表 10.1. 可用的systemd单位类型

Unit TypeFile ExtensionDescription
Service unit .service A system service.
Target unit .target A group of systemd units.
Automount unit .automount A file system automount point.
Device unit .device A device file recognized by the kernel.
Mount unit .mount A file system mount point.
Path unit .path A file or directory in a file system.
Scope unit .scope An externally created process.
Slice unit .slice A group of hierarchically organized units that manage system processes.
Snapshot unit .snapshot A saved state of the systemd manager.
Socket unit .socket An inter-process communication socket.
Swap unit .swap A swap device or a swap file.
Timer unit .timer A systemd timer.

表10.2。系统单元文件位置

目录描述
/usr/lib/systemd/system/ 使用已安装的RPM软件包分发的系统单元文件。
/run/systemd/system/ 在运行时创建的系统单元文件。此目录优先于已安装服务单元文件的目录。
/etc/systemd/system/ 创建的系统单元文件systemctl enable以及为扩展服务而添加的单元文件。此目录优先于具有运行时单元文件的目录。

通过该RPM 方式安装的软件包的unit 文件保存目录:   /usr/lib/systemd/system/

#目录下包含许多以 .service 结尾的文件
[root@localhost system]# cd /usr/lib/systemd/system/
[root@localhost system]# ls arp-ethers.service reboot.target auditd.service reboot.target.wants autovt@.service remote-cryptsetup.target basic.target remote-fs-pre.target basic.target.wants remote-fs.target blk-availability.service rescue.service bluetooth.target rescue.target brandbot.path rescue.target.wants brandbot.service rhel-autorelabel-mark.service chrony-dnssrv@.service rhel-autorelabel.service chrony-dnssrv@.timer rhel-configure.service chronyd.service rhel-dmesg.service chrony-wait.service rhel-domainname.service console-getty.service rhel-import-state.service

systemd系统和服务管理器提供以下主要功能:
systemd系统和服务管理器提供以下主要功能:
Socket-based :基于套接字的激活 - 在引导时,systemd为支持此类激活的所有系统服务创建侦听套接字,并在它们启动后立即将套接字传递给这些服务。这不仅允许systemd并行启动服务,而且还可以重新启动服务,而不会丢失任何在不可用时发送给它的消息:相应的套接字仍然可访问,所有消息都排队。
Systemd :Systemd使用套接字单元进行基于套接字的激活。
Bus :基于总线的激活 - 在客户端应用程序第一次尝试与它们通信时,可以按需启动使用D-Bus进行进程间通信的系统服务。Systemd使用 D-Bus服务文件进行基于总线的激活。
Device :基于设备的激活 - 当插入特定类型的硬件或可用时,可以按需启动支持基于设备的激活的系统服务。Systemd使用设备单元进行基于设备的激活。
Path :基于路径的激活 - 当特定文件或目录更改其状态时,可以按需启动支持基于路径的激活的系统服务。Systemd使用路径单元进行基于路径的激活。
Mount :挂载和自动挂载点管理 - Systemd监视并管理挂载和自动挂载点。Systemd使用装载单元作为挂载点,使用自动挂载单元作为自动挂载点。
Aggressive parallelization:积极的并行化 - 由于使用基于套接字的激活,systemd可以在所有侦听套接字到位后立即并行启动系统服务。结合支持按需激活的系统服务,并行激活可显着减少引导系统所需的时间。
Transactional unit activation logic :事务单元激活逻辑 - 在激活或停用单元之前,systemd计算其依赖关系,创建临时事务,并验证此事务是否一致。如果事务不一致,systemd会在报告错误之前自动尝试更正它并从中删除非必要的作业。
Backwards compatibility with SysV init: 向后兼容SysV init - Systemd支持SysV init脚本,如 Linux标准基本核心规范中所述,这简化了系统服务单元的升级路径。

 一般我们常见的是这四种:

Service unit            .service	
Target unit	       .target	
Device unit	      .device	
Mount unit	         .mount

  

我们通过yum 安装一个 apache 看下 unit 文件:

[root@localhost system]# ll http*
-rw-r--r-- 1 root root 752 4月  24 09:44 httpd.service
[root@localhost system]# cat httpd.service 
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target
[root@localhost system]# 

整个文件分三个部分,[Unit]·[Service]·[Install]

[Unit]:记录unit文件的通用信息和控制。

[Service]:记录Service的信息

[Install]:安装信息。

1.定义控制单元 [Unit]

  • 在 Systemd 中,所有引导过程中 Systemd 要控制的东西都是一个单元。基本的用法如下:
  • Description:代表整个单元的描述,可根据需要任意填写。
  • Wants:本单元启动了,它“想要”的单元也会被启动。但是这个单元若启动不成功,对本单元没有影响。
  • Requires: 这个单元启动了,那么它“需要”的单元也会被启动; 它“需要”的单元被停止了,它自己也活不了。但是请注意,这个设定并不能控制启动顺序,因为它“需要”的单元启动也需要时间,若它“需要”的单元启动还未完成,就开始启动本单元,则本单元也无法启动,所以不建议使用这个字段。
  • OnFailure:若本单元启动失败了,那么启动这个单元作为折衷。
  • Before/After:指定启动顺序。
[Unit]           #Unit 文件通用信息
Description=The Apache HTTP Server #描述
After=network.target remote-fs.target nss-lookup.target #表示在这些文件启动后才能够启动,还有Before 表示在哪些文件启动前启动
Documentation=man:httpd(8)       #手册信息   
Documentation=man:apachectl(8)    #手册信息

  

2.定义服务本体 [service]

[Service]
Type=notify   #服务的类型
EnvironmentFile=/etc/sysconfig/httpd   #指定环境列表的文件
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND   #这个参数是几乎每个 .service 文件都会有的,指定服务启动的主要命令,在每个配置文件中只能使用一次。
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful     #重新加载服务所需执行的主要命令。
ExecStop=/bin/kill -WINCH ${MAINPID}               #停止服务所需要执行的主要命令
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT      #      让一个停止(stopped)的进程继续执行. 本信号不能被阻塞. 可以用一个handler来让程序在由stopped状态变为继续执行时完成特定的工作. 例如, 重新显示提示符额,优雅的停止
PrivateTmp=true      #采用布尔参数。如果为true,则为执行的进程设置新的文件系统名称空间,并在其中安装不由名称空间外的进程共享的私有/tmp/var/tmp目录。这对于保护对进程的临时文件的访问非常有用

  

 

3.[Install]段

Alias为单元提供一个空间分离的附加名字。
RequiredBy单元被允许运行需要的一系列依赖单元,RequiredBy列表从Require获得依赖信息。
WantBy单元被允许运行需要的弱依赖性单元,Wantby从Want列表获得依赖信息。
Also指出和单元一起安装或者被协助的单元。
DefaultInstance实例单元的限制,这个选项指定如果单元被允许运行默认的实例。

[Install]
WantedBy=multi-user.target      #系统启动需要用户级别3 
#multi-user.target是systemV世界中运行级别3的替代方案

  

 小测试:

执行systemctl enable apache.service 时 默认会去/usr/lib/systemd/system/ 目录下找文件,直接放在 /etc/systemd/system/  目录下不行
[root@localhost system]# ll apache.service 
-rw-r--r-- 1 root root 672 7月   2 05:12 apache.service
[root@localhost system]# systemctl enable apache.service  #执行  systemctl enable 命令 后会自动创建一个链接指向 /etc/systemd/system/ 目录下
Created symlink from /etc/systemd/system/multi-user.target.wants/apache.service to /usr/lib/systemd/system/apache.service.
[root@localhost system]# systemctl start apache.service  #启动
[root@localhost system]# systemctl status apache.service #状态
● apache.service - The Apache HTTP Server Unit test
   Loaded: loaded (/usr/lib/systemd/system/apache.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2019-07-02 05:13:15 EDT; 7s ago
 Main PID: 17799 (httpd)
    Tasks: 6
   Memory: 2.7M
   CGroup: /system.slice/apache.service
           ├─17799 /usr/sbin/httpd -DFOREGROUND
           ├─17800 /usr/sbin/httpd -DFOREGROUND
           ├─17801 /usr/sbin/httpd -DFOREGROUND
           ├─17802 /usr/sbin/httpd -DFOREGROUND
           ├─17803 /usr/sbin/httpd -DFOREGROUND
           └─17804 /usr/sbin/httpd -DFOREGROUND

7月 02 05:13:15 localhost.localdomain systemd[1]: Started The Apache HTTP Server Unit test.
7月 02 05:13:16 localhost.localdomain httpd[17799]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive global...s this message
Hint: Some lines were ellipsized, use -l to show in full.
*****************************************************************************************
这个文件是修改了 httpd.service 文件得来,删除了一点东西
[root@localhost system]# cat apache.service [Unit] Description=The Apache HTTP Server Unit test After=network.target [Service] Type=simple EnvironmentFile=/etc/sysconfig/httpd ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND ExecReload=/usr/sbin/httpd $OPTIONS -k graceful ExecStop=/bin/kill -WINCH ${MAINPID} # We want systemd to give httpd some time to finish gracefully, but still want # it to kill httpd after TimeoutStopSec if something went wrong during the # graceful stop. Normally, Systemd sends SIGTERM signal right after the # ExecStop, which would kill httpd. We are sending useless SIGCONT here to give # httpd time to finish. #KillSignal=SIGCONT #PrivateTmp=true [Install] WantedBy=multi-user.target [root@localhost system]# pwd /usr/lib/systemd/system [root@localhost system]#

  

 

  

  

原文地址:https://www.cnblogs.com/zy09/p/11120453.html