AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1261340
Accepted
mao
mao
Asked: 2020-07-24 00:17:06 +0800 CST2020-07-24 00:17:06 +0800 CST 2020-07-24 00:17:06 +0800 CST

防止 systemd 服务以恢复模式启动

  • 772

我有一个 systemd 服务,它具有:

[Unit]

After=network-online.target krb5-kdc.service krb5-admin-server.service

Wants=krb5-kdc.service network-online.target krb5-admin-server.service

除非网络堆栈完全准备好/完全可用,并且 Kerberos 服务已启动,否则该服务不应启动。(这就是我想要实现的,当这两个都不正确时,我的服务不应该尝试启动)。

我的单元文件似乎可以正常使用。

重新启动时,从 grub2 菜单中,如果我选择“* Ubuntu 的高级选项”,然后为我的内核选择“恢复模式”,然后打开 root shell,并且不运行“启用网络”,我可以看到我的服务尝试启动(随后失败,因为它需要完全运行的网络以及 kerberos 服务)。

除非网络堆栈完全可用并且 kerberos 服务已启动,否则我必须更改哪些内容才能启动服务?

在恢复模式 root shell 中(没有“启用网络”):

systemctl list-units --type target

UNIT                     LOAD   ACTIVE SUB    DESCRIPTION
basic.target             loaded active active Basic System
cryptsetup.target        loaded active active Encrypted Volumes
friendly-recovery.target loaded active active Friendly Recovery Mode
local-fs-pre.target      loaded active active Local File Systems (Pre)
local-fs.target          loaded active active Local File Systems
network-online.target    loaded active active Network is Online
network-pre.target       loaded active active Network (Pre)
network.target           loaded active active Network
paths.target             loaded active active Paths
slices.target            loaded active active Slices
sockets.target           loaded active active Sockets
sound.target             loaded active active Sound Card
swap.target              loaded active active Swap
sysinit.target           loaded active active System Initialization
time-sync.target         loaded active active System Time Synchronized
timers.target            loaded active active Timers

在上面的列表中,我没有看到任何 kerberos 服务,也不知道为什么目标 network.online 处于活动状态,它的含义一定与我的假设不同。

在引导到恢复模式期间,我还在控制台上看到:

[ DEPEND ] Dependency failed for  Network Manager wait online
...
Reached Target Network is online

但是,网络没有以正常方式运行:我无法 ping 此主机名的静态 IP4 地址(结果: 网络无法访问)。尽管我可以 ping 127.0.0.1,但我无法引用本地网络上的任何其他计算机。所以网络没有完全运行,这就是我想要避免的,即我希望我的服务只在网络完全运行时启动。

似乎我还没有理解目标网络在线的真正含义?

当我的服务尝试启动时,它会因为相关服务不可用而超时,所以systemctl status myservice.service显示

● myservice.service - description
   Loaded: loaded (/etc/systemd/system/myservice.service; enabled; vendor preset: enabled)
   Active: activating (auto-restart) (Result: timeout) since Fri 2020-07-24 16:40:45 BST; 7s ago
  Process: 1396 ExecStartPre=/usr/local/bin/get_kerberos_tickets.ksh (code=killed, signal=HUP)
    Tasks: 0
   Memory: 3.4M
      CPU: 529ms

Jul 24 16:40:45 t7810 systemd[1]: myservice.service: Unit entered failed state.
Jul 24 16:40:45 t7810 systemd[1]: myservice.service: Failed with result 'timeout'.

如果我修改我的单元文件以添加multi-user.target到 WANTS 和 AFTER 行,那么我的服务也会尝试启动,即使 root shell 然后显示systemctl list-units -target有 multi-user.target “加载非活动死”。

systemd recovery-mode 16.04
  • 1 1 个回答
  • 640 Views

1 个回答

  • Voted
  1. Best Answer
    abu_bua
    2020-07-25T08:23:32+08:002020-07-25T08:23:32+08:00

    系统加载恢复模式network-online.target。

    运行命令

    systemctl list-units --type target
    

    列出在恢复模式下加载的目标。

    线

    network-online.target    loaded active active Network is Online
    

    显示网络服务已加载。因此,您的系统按预期运行。

    一个想法是使用Conflicts, 从systemd.units(7)

    Conflicts=
           A space-separated list of unit names. Configures negative requirement dependencies. If a unit has a
           Conflicts= setting on another unit, starting the former will stop the latter and vice versa.
    
           Note that this setting does not imply an ordering dependency, similarly to the Wants= and Requires=
           dependencies described above. This means that to ensure that the conflicting unit is stopped before the
           other unit is started, an After= or Before= dependency must be declared. It doesn't matter which of the
           two ordering dependencies is used, because stop jobs are always ordered before start jobs, see the
           discussion in Before=/After= below.
    
           If unit A that conflicts with unit B is scheduled to be started at the same time as B, the transaction
           will either fail (in case both are required parts of the transaction) or be modified to be fixed (in case
           one or both jobs are not a required part of the transaction). In the latter case, the job that is not
           required will be removed, or in case both are not required, the unit that conflicts will be started and
           the unit that is conflicted is stopped.
    

    在 grub 中进入恢复模式时,systemd 开始到达friendly-recovery.target。因此,请尝试在您的服务单元中设置此语句:

    Conflicts=friendly-recovery.target
    

    这应该会阻止在恢复模式下启动您的服务。

    • 1

相关问题

  • 当 GRUB 恢复菜单无法加载 shell 时如何引导到 root shell?

  • Grub 启动进入恢复模式

  • 命令列出启动时启动的服务?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve