最新消息:运维之路

Centos7服务管理之Systemctl

系统运维 admin 52浏览 0评论

systemd

1.介绍

Systemd是Linux操作系统的系统和服务管理器。 它被设计为向后兼容SysV初始化脚本,并提供了许多功能,例如在启动时并行启动系统服务,按需激活守护程序,支持系统状态快照或基于依赖关系的服务控制逻辑。 在Red Hat Enterprise Linux 7中,systemd将Upstart替换为默认的init系统。(这段介绍来自官方翻译…..从Centos7开始 systemd开始接管取代service管理linux各项服务啦)
2.用法

重启系统

$ sudo systemctl reboot

关闭系统,切断电源

$ sudo systemctl poweroff

CPU停止工作

$ sudo systemctl halt

暂停系统

$ sudo systemctl suspend

让系统进入冬眠状态

$ sudo systemctl hibernate

让系统进入交互式休眠状态

$ sudo systemctl hybrid-sleep

启动进入救援状态(单用户状态)

$ sudo systemctl rescue

1.启动nfs服务

systemctl start nfs-server.service

2.设置开机自启动

systemctl enable nfs-server.service

3.停止开机自启动

systemctl disable nfs-server.service

4.查看服务当前状态

systemctl status nfs-server.service

5.重新启动某服务

systemctl restart nfs-server.service

6.查看所有已启动的服务

systemctl list -units –type=service

 

3.创建和修改系统单元文件

 

脚本目录:
/usr/lib/systemd/
服务配置文件目录(开机自启动):
/usr/lib/systemd/system
服务文件
服务格式:
*.service
服务文件格式:
[Unit] //用户服务说明
Description=xxx //描述服务
After=xxx //After设置确保仅在网络运行后才启动该服务(翻译官方文档 并非网上乱写说是描述类别)
[Service] //服务具体运行参数
Type=forking //服务后台运行
PIDFile=/path/pid //PID的路径
ExecStart= //服务运行的命令与参数(绝对路径)
ExecReload= //服务重启的命令与参数(绝对路径)
ExecStop= //服务停止运行的命令与参数(绝对路径)
PrivateTmp=True //为服务分配独立的临时空间
[Install] //服务安装的配置

 

从官方摘几个原版的东东 有空再翻译…

Table 9.9. Important [Unit] Section Options

[b] In most cases, it is sufficient to set only the ordering dependencies with After and Before unit file options. If you also set a requirement dependency with Wants (recommended) or Requires, the ordering dependency still needs to be specified. That is because ordering and requirement dependencies work independently from each other.
Option[a] Description
Description A meaningful description of the unit. This text is displayed for example in the output of the systemctl status command.
Documentation Provides a list of URIs referencing documentation for the unit.
After[b] Defines the order in which units are started. The unit starts only after the units specified in After are active. Unlike Requires, After does not explicitly activate the specified units. The Before option has the opposite functionality to After.
Requires Configures dependencies on other units. The units listed in Requires are activated together with the unit. If any of the required units fail to start, the unit is not activated.
Wants Configures weaker dependencies than Requires. If any of the listed units does not start successfully, it has no impact on the unit activation. This is the recommended way to establish custom unit dependencies.
Conflicts Configures negative dependencies, an opposite to Requires.
[a] For a complete list of options configurable in the [Unit] section, see the systemd.unit(5) manual page.

 

Table 9.10. Important [Service] Section Options

Option[a] Description
Type Configures the unit process startup type that affects the functionality of ExecStartand related options. One of:

  • simple – The default value. The process started with ExecStart is the main process of the service.
  • forking – The process started with ExecStart spawns a child process that becomes the main process of the service. The parent process exits when the startup is complete.
  • oneshot – This type is similar to simple, but the process exits before starting consequent units.
  • dbus – This type is similar to simple, but consequent units are started only after the main process gains a D-Bus name.
  • notify – This type is similar to simple, but consequent units are started only after a notification message is sent via the sd_notify() function.
  • idle – similar to simple, the actual execution of the service binary is delayed until all jobs are finished, which avoids mixing the status output with shell output of services.
ExecStart Specifies commands or scripts to be executed when the unit is started. ExecStartPre and ExecStartPost specify custom commands to be executed before and after ExecStart. Type=oneshot enables specifying multiple custom commands that are then executed sequentially.
ExecStop Specifies commands or scripts to be executed when the unit is stopped.
ExecReload Specifies commands or scripts to be executed when the unit is reloaded.
Restart With this option enabled, the service is restarted after its process exits, with the exception of a clean stop by the systemctl command.
RemainAfterExit If set to True, the service is considered active even when all its processes exited. Default value is False. This option is especially useful if Type=oneshot is configured.
[a] For a complete list of options configurable in the [Service] section, see the systemd.service(5) manual page.

 

Table 9.11. Important [Install] Section Options

Option[a] Description
Alias Provides a space-separated list of additional names for the unit. Most systemctlcommands, excluding systemctl enable, can use aliases instead of the actual unit name.
RequiredBy A list of units that depend on the unit. When this unit is enabled, the units listed in RequiredBy gain a Require dependency on the unit.
WantedBy A list of units that weakly depend on the unit. When this unit is enabled, the units listed in WantedBy gain a Want dependency on the unit.
Also Specifies a list of units to be installed or uninstalled along with the unit.
DefaultInstance Limited to instantiated units, this option specifies the default instance for which the unit is enabled. See Section 9.6.5, “Working with Instantiated Units”
[a] For a complete list of options configurable in the [Install] section, see the systemd.unit(5) manual page.

转载请注明:DevOps » Centos7服务管理之Systemctl

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址