设为首页 加入收藏

TOP

`systemctl` 启动单个服务,其中包含多个进程
2023-07-23 13:29:47 】 浏览:16
Tags:systemctl 单个服 包含多

要使用 systemctl 启动单个服务,其中包含多个进程,你可以使用 Systemd 的 template 机制。以下是使用 Systemd 'template' 以创建一个可同时启动多个进程的服务单元文件的过程:

  1. 为你的服务创建一个 template 服务单元文件。服务单元文件通常位于 /etc/systemd/system 目录中。使用以下命令创建一个 template 服务单元文件:

    sudo touch /etc/systemd/system/your-service@.service
    

    注意 @ 符号,它表示这是一个模板服务文件。根据实际服务名称替换 your-service

  2. 编辑 template 服务单元文件。使用文本编辑器(如 nanovim)打开服务单元文件,并为该服务配置适当的设置。

    sudo nano /etc/systemd/system/your-service@.service
    

    添加以下内容,根据您的实际需求进行修改:

    [Unit]
    Description=Description of your service instance %i
    After=network.target
    
    [Service]
    User=user-to-run-the-service-instance
    Group=group-to-run-the-service-instance
    ExecStart=/path/to/your/program --program-options instance-%i
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    

    DescriptionUserGroupExecStart 替换为适当的值。请特别注意,我们添加了 %i 标记。这将在启动服务时替换为实例编号。

  3. 重新加载 Systemd 配置:

    sudo systemctl daemon-reload
    
  4. 使用 systemctl 启动具有多个进程的单个服务:

    sudo systemctl start your-service@1 your-service@2 your-service@3
    

    在上述命令中,将 your-service 替换为您在步骤 1 中创建的模板服务文件的名称,并使用所需的实例数量。这将启动多个进程,每个进程都具有不同的实例编号。

现在,你已经配置了一个包含多个进程的服务,并使用 systemctl 将它们同时启动。使用此方法,你可以根据需要动态地启动、停止或重启各个进程实例。

要在系统启动时自动运行这些服务实例,请分别为每个指定实例使用如下命令:

sudo systemctl enable your-service@1
sudo systemctl enable your-service@2
sudo systemctl enable your-service@3

替换 your-service 并指定所需实例号。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇linux 条件语句和逻辑判断 下一篇协程概述讲解

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目