Systemd“别名”启用状态

Systemd“别名”启用状态

有人可以给出“别名”状态下的单元文件的示例吗?

文档说:“该名称是别名(到另一个单元文件的符号链接)”

但是,据我了解,如果单元文件未启用,并且它是符号链接,那么它要么是指向 systemd 范围之外的文件的符号链接(因此处于“链接”状态),要么是指向内部文件的符号链接systemd 达到(因此处于“间接”状态)。

编辑:更具体地说,假设我有这个示例服务:

$ cat myservice.sh 
while true
do
echo "Looping"
sleep 30
done

以及单元文件:

$ cat myservice.service
[Unit]
Description=Example Service.

[Service]
Type=simple
ExecStart=/bin/bash ~/myservice.sh

[Install]
WantedBy=multi-user.target

如果我想为 做一个别名myservice.service,这是一个单位,使得的myalias.service -> myservice.service输出为,我该怎么办?systemctl is-enabled myaliasalias

答案1

systemd.special(7)列出了几个应该使用别名的单位。

手册页中的一个示例是:

   default.target
      The default unit systemd starts at bootup. Usually, this should be
      aliased (symlinked) to multi-user.target or graphical.target. See
      bootup(7) for more discussion.

      The default unit systemd starts at bootup can be overridden with the 
      systemd.unit= kernel command line option, or more conveniently, with 
      the short names like single, rescue, 1, 3, 5, ...; see systemd(1).

如果我检查 Debian 测试的单元文件,我会看到:

$ systemctl cat default.target
# /lib/systemd/system/graphical.target
#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Graphical Interface
Documentation=man:systemd.special(7)
Requires=multi-user.target
Wants=display-manager.service
Conflicts=rescue.service rescue.target
After=multi-user.target rescue.service rescue.target display-manager.service
AllowIsolate=yes

$ systemctl status default.target
● graphical.target - Graphical Interface
     Loaded: loaded (/lib/systemd/system/graphical.target; static)
     Active: active since Tue 2020-08-18 08:01:45 CEST; 4 days ago
       Docs: man:systemd.special(7)

Aug 18 08:01:45 stewbian systemd[1]: Reached target Graphical Interface.

$ systemctl is-enabled default.target
alias

如果我们想查看其他别名别名,我们可以ls目录并 grep 所有不是掩码的链接:

$ ls -l /lib/systemd/system/ | grep -e '^l' | grep -v '/dev/null'
lrwxrwxrwx [email protected] -> [email protected]
lrwxrwxrwx ctrl-alt-del.target -> reboot.target
lrwxrwxrwx dbus-org.freedesktop.hostname1.service -> systemd-hostnamed.service
lrwxrwxrwx dbus-org.freedesktop.locale1.service -> systemd-localed.service
lrwxrwxrwx dbus-org.freedesktop.login1.service -> systemd-logind.service
lrwxrwxrwx dbus-org.freedesktop.timedate1.service -> systemd-timedated.service
lrwxrwxrwx default.target -> graphical.target
lrwxrwxrwx gdm3.service -> gdm.service
lrwxrwxrwx kmod.service -> systemd-modules-load.service
lrwxrwxrwx plymouth-log.service -> plymouth-read-write.service
lrwxrwxrwx plymouth.service -> plymouth-quit.service
lrwxrwxrwx procps.service -> systemd-sysctl.service
lrwxrwxrwx runlevel0.target -> poweroff.target
lrwxrwxrwx runlevel1.target -> rescue.target
lrwxrwxrwx runlevel2.target -> multi-user.target
lrwxrwxrwx runlevel3.target -> multi-user.target
lrwxrwxrwx runlevel4.target -> multi-user.target
lrwxrwxrwx runlevel5.target -> graphical.target
lrwxrwxrwx runlevel6.target -> reboot.target
lrwxrwxrwx speech-dispatcher.service -> speech-dispatcherd.service
lrwxrwxrwx udev.service -> systemd-udevd.service

我的机器上唯一的非静态服务示例是speech-dispatcher.service

$ systemctl is-enabled speech-dispatcher.service
alias
$ systemctl status speech-dispatcher.service
● speech-dispatcherd.service - Speech-Dispatcher, common interface to speech synthesizers
     Loaded: loaded (/lib/systemd/system/speech-dispatcherd.service; disabled; vendor preset: enabled)
     Active: inactive (dead)

您会注意到它已被禁用。如果我启用它,我会得到:

$ systemctl enable speech-dispatcher.service
Synchronizing state of speech-dispatcher.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable speech-dispatcher
Created symlink /etc/systemd/system/speech-dispatcher.service → /lib/systemd/system/speech-dispatcherd.service.
Created symlink /etc/systemd/system/multi-user.target.wants/speech-dispatcherd.service → /lib/systemd/system/speech-dispatcherd.service.

$ systemctl status speech-dispatcher.service
● speech-dispatcherd.service - Speech-Dispatcher, common interface to speech synthesizers
     Loaded: loaded (/lib/systemd/system/speech-dispatcherd.service; enabled; vendor preset: enabled)
     Active: inactive (dead)

stew ~ $ systemctl is-enabled speech-dispatcher.service
alias

相关内容