linux 下的 service 和systemctl 服务管理方式html
man service 获得:执行一个system V 风格的启动脚本
linux
service - run a System V init scriptshell
man systemctl 获得: 控制systemd 系统和服务管理器apache
systemctl - Control the systemd system and service managerbash
相关链接机官方文档:并发
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.pdfless
手册说明 有些官方找不到的参数这里查 :https://www.freedesktop.org/software/systemd/man/systemd.exec.htmldom
init(为英语:initialization的简写)是 Unix 和 类Unix 系统中用来产生其它全部进程的程序。它以守护进程的方式存在,其进程号为1。Linux系统在引导时加载Linux内核后,便由Linux内核加载init程序,由init程序完成余下的引导过程,好比加载运行级别,加载服务,引导Shell/图形化界面等等。socket
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。ide
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 Type | File Extension | Description |
---|---|---|
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]:安装信息。
[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) #手册信息
[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
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]#