systemd 和 如何修改和创建一个 systemd service (Understanding and administering systemd)

Posted xuyaowen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了systemd 和 如何修改和创建一个 systemd service (Understanding and administering systemd)相关的知识,希望对你有一定的参考价值。

系统中经常会使用到 systemctl 去管理systemd程序,刚刚看了一篇关于 systemd 和 SysV 相关的文章,这里简要记录一下:

systemd定义: (英文来解释更为原汁原味)

systemd is a system and service manager for Linux, compatible with SysV and LSB init scripts. systemd provides:

  • Aggressive parallelization capabilities

  • Uses socket and D-Bus activation for starting services

  • Offers on-demand starting of daemons, keeps track of processes using Linux cgroups

  • Supports snapshotting and restoring of the system state

  • Maintains mount and automount points

  • Implements an elaborate transactional dependency-based service control logic.

The systemctl command is the primary tool to manage systemd. It combines the functionality of SysVinit’s service and chkconfig commands into a single tool you can use to enable and disable services permanently or only for the current session.

systemd manages units, which are representations of system resources and services. This following list shows the unit types that systemd can manage:

service

A service on the system, including instructions for starting, restarting, and stopping the service.

socket

A network socket associated with a service.

device

A device specifically managed with systemd.

mount

A mountpoint managed with systemd.

automount

A mountpoint automatically mounted on boot.

swap

Swap space on the system.

target

A synchronization point for other units. Usually used to start enabled services on boot.

path

A path for path-based activation. For example, you can start services based on the state of a certain path, such as whether it exists or not.

timer

A timer to schedule activation of another unit.

snapshot

A snapshot of the current systemd state. Usually used to rollback after making temporary changes to systemd.

slice

Restrivtion of resources through Linux Control Group nodes (cgroups).

scope

Information from systemd bus interfaces. Usually used to manage external system processes.

systemctl 常用管理命令:

# systemctl start foo
# systemctl stop foo
# systemctl restart foo
# systemctl status foo
# systemctl enable foo
# systemctl disable foo
# systemctl mask foo
# systemctl is-enabled foo

systemctl service 创建:

systemctl service在两个目录中: /etc/systemd/system 和 /usr/lib/systemd/system 之中

我们一般自己创建的service 直接放在 /etc/systemd/system 之中即可:

vim xuyaowen.service 创建文件

输入如下内容:

[Unit]
Description=xuyaowen custom service
Requires=network.target

[Service]
Type=simple
ExecStart=/usr/bin/sleep infinity

[Install]
WantedBy=multi-user.target

保存退出后,使用 systemctl start xuyaowen 启动后,使用 systemctl status 查看service 状态:

?  system systemctl status xuyaowen.service    
● xuyaowen.service - xuyaowen custom service
   Loaded: loaded (/etc/systemd/system/xuyaowen.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2018-08-16 10:44:05 CST; 38min ago
 Main PID: 19556 (sleep)
    Tasks: 1 (limit: 4915)
   Memory: 172.0K
   CGroup: /system.slice/xuyaowen.service
           └─19556 /usr/bin/sleep infinity

Aug 16 10:44:05 yaowenxu systemd[1]: Started xuyaowen custom service.

service 创建成功。具体systemd 与 SysV 之间的关系可以在网络上较为容易查找到,我这里只是作为简要记录。关于systemd 更为详细的内容,请参考 fedora doc .


 保持更新,转载请注明出处。如果您觉得本文对你有帮助请点右下角推荐,谢谢

以上是关于systemd 和 如何修改和创建一个 systemd service (Understanding and administering systemd)的主要内容,如果未能解决你的问题,请参考以下文章

centos7 中 systemd systemctl管理服务的命令

如何通过 python 和 dbus (systemd-run like) 创建 systemd 瞬态计时器和服务?

Ubuntu 18.04 rc.local systemd设置

(转)systemctl 命令完全指南

Linux Simple Systemd Service Guide

centos 安装桌面,启动。