为systemd添加一个服务

新建一个服务

systemdunit文件分布在

系统模式

  • /usr/lib/systemd/system/ 软件包安装

  • /etc/systemd/system/ 系统管理员维护

用户模式

  • /usr/lib/systemd/user/ 软件包安装

  • /etc/systemd/user/ 管理员维护的用户服务

  • ~/.config/systemd/user/ 用户自身的服务

新建服务时,新建一个sample.service,比如要实现重启防火墙的功能,格式如下

[Unit]
Description=Restart firewalld.service # 描述
After=ovirt-engine.service # 服务在其他服务之后启动
Requires=ovirt-engine.service # 服务需要其他服务启动后启动

[Service]
Type=oneshot # 类型,只运行一次
ExecStartPre=/usr/bin/env systemctl stop firewalld # 启动之前执行
ExecStart=/usr/bin/env systemctl start firewalld # 启动执行
KillMode=none # 重启模式
Restart=no # 重启

[Install]
WantedBy=multi-user.target # 安装时依赖的目标

添加好需要的service文件之后,使用

systemctl daemon-reload 

重新加载所有的service

使用systemctl list-units --type=service,可以查询到刚才添加的服务

使用systemctl enable sample使其开机加载

参考资料










相关文章

LinuxSystemd服务(2021.07.09)目录LinuxSystemd服务(2021.07...
opidrvabortingprocessM002ospid(3561)asaresultofORA-600OR...
安装好haproxy后,配置正确无法启动,看日志:Feb1309:32:50clu...
Linux 系统与服务管理工具Systemd被曝存在3大漏洞,影响几乎...
一、systemd查看日志文件有隐藏 systemctlstatusSERVICE-l-...
不要在mp目录下保存文件,该目录会定期清理文件mp默认保存10...