加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

为systemd添加一个服务

发布时间:2020-12-14 15:31:30 所属栏目:百科 来源:网络整理
导读:新建一个服务 systemd 的 unit 文件分布在 系统模式 /usr/lib/systemd/system/ 软件包安装 /etc/systemd/system/ 系统管理员维护 用户模式 /usr/lib/systemd/user/ 软件包安装 /etc/systemd/user/ 管理员维护的用户服务 ~/.config/systemd/user/ 用户自身的

新建一个服务

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使其开机加载

参考资料










(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读