systemd 和 如何修改和创建一个 systemd service…

2018-08-17 09:31:59来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

系统中经常会使用到 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 .


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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Linux Ubuntu 安装、汉化、常用操作

下一篇:蓝牙stack bluez学习(1)Stack Architecture