lsyncd 启用的服务不会在 PC 重启时自动启动 - 活动:不活动(已停止)

lsyncd 启用的服务不会在 PC 重启时自动启动 - 活动:不活动(已停止)

我通过合并所有之前的编辑,大大简化了这个问题

lsyncd 配置文件/etc/lsyncd/lsyncd.conf.lua

settings {
    logfile = "/media/blueray/WDRed/_LsyncLog/lsyncd.log",
    statusFile = "/media/blueray/WDRed/_LsyncLog/lsyncd-status.log",
    pidfile="/media/blueray/WDRed/_LsyncLog/lsyncd.pid",
    statusInterval = 5,
    nodaemon = false,
}

sync {
    default.rsync,
    source    = "/media/blueray/Data/",
    target    = "/media/blueray/WDRed/_LsyncBackup/",
    delay     = 0, 
    exclude = { "/_RedoBackup" },
    rsync     = {
        binary   = "/usr/bin/rsync",
        archive  = true,
        compress = true,
        _extra = { "--backup-dir=/media/blueray/WDRed/_LsyncDeletedBackup/" },
    }
}

lsyncd 服务文件/home/blueray/.config/systemd/user/lsyncd.service

[Unit]
Description=Live Syncing (Mirror) Daemon
After=network.target remote-fs.target media-blueray-Data.mount media-blueray-WDRed.mount
Wants=media-blueray-Data.mount media-blueray-WDRed.mount
Requires=media-blueray-Data.mount media-blueray-WDRed.mount

[Service]
Type=forking
ExecStart=/usr/bin/lsyncd /etc/lsyncd/lsyncd.conf.lua
PIDFile=/media/blueray/WDRed/_LsyncLog/lsyncd.pid
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill $MAINPID
Restart=always

[Install]
WantedBy=multi-user.target

重启之前

blueray@blueray-i58600K:~$ systemctl --user start lsyncd
blueray@blueray-i58600K:~$ systemctl --user enable lsyncd
blueray@blueray-i58600K:~$ systemctl --user status lsyncd
● lsyncd.service - Live Syncing (Mirror) Daemon
   Loaded: loaded (/home/blueray/.config/systemd/user/lsyncd.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2018-10-29 11:58:22 +06; 11s ago
 Main PID: 1921 (lsyncd)
   CGroup: /user.slice/user-1000.slice/[email protected]/lsyncd.service
           └─1921 /usr/bin/lsyncd /etc/lsyncd/lsyncd.conf.lua

Oct 29 11:58:22 blueray-i58600K systemd[916]: Starting Live Syncing (Mirror) Daemon...
Oct 29 11:58:22 blueray-i58600K systemd[916]: Started Live Syncing (Mirror) Daemon.
blueray@blueray-i58600K:~$ 

重启后

blueray@blueray-i58600K:~$ systemctl --user status lsyncd
● lsyncd.service - Live Syncing (Mirror) Daemon
   Loaded: loaded (/home/blueray/.config/systemd/user/lsyncd.service; enabled; vendor preset: enabled)
   Active: inactive (dead)
blueray@blueray-i58600K:~$ 

如您所见,如果我给出命令,此服务运行良好systemctl --user start lsyncd

为什么我已给出命令但还是没有自动启动。为什么重启后才systemctl --user enable lsyncd显示?Active: inactive (dead)

答案1

Systemd 可能会与之前和之后的版本混淆,但我会改变:

After=network.target remote-fs.target media-blueray-Data.mount media-blueray-WDRed.mount

到:

After=network.target remote-fs.target media-blueray-Data.mount media-blueray-WDRed.mount network.target multi-user.target

基于此回答

答案2

创建 lsyncd 配置文件(您也可以将其放在其他位置)/home/blueray/.config/lsyncd.conf.lua

settings {
    logfile = "/media/blueray/WDRed/_LsyncLog/lsyncd.log",
    statusFile = "/media/blueray/WDRed/_LsyncLog/lsyncd-status.log",
    pidfile="/home/blueray/.config/lsyncd.pid",
    statusInterval = 5,
    nodaemon = false,
}

sync {
    default.rsync,
    source    = "/media/blueray/Data/",
    target    = "/media/blueray/WDRed/_LsyncBackup/",
    delay     = 0, 
    exclude = { "/_RedoBackup" },
    rsync     = {
        binary   = "/usr/bin/rsync",
        archive  = true,
        compress = true,
        _extra = { "--backup-dir=/media/blueray/WDRed/_LsyncDeletedBackup/" },
    }
}

在创建 lsyncd 服务文件/home/blueray/.config/systemd/user/lsyncd.service

[Unit]
Description=Live Syncing (Mirror) Daemon
Requires=default.target
After=default.target

[Service]
Type=forking
ExecStart=/usr/bin/lsyncd /home/blueray/.config/lsyncd.conf.lua
PIDFile=/home/ismail/.config/lsyncd.pid
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill $MAINPID
Restart=always
RestartSec=10

[Install]
WantedBy=default.target

调试步骤动机:

由于这类问题困扰着互联网——

https://superuser.com/questions/955922/enabled-systemd-unit-does-not-start-at-boot

https://unix.stackexchange.com/questions/237795/lsyncd-wont-start-at-startup

https://unix.stackexchange.com/questions/374638/why-x0vncserver-is-not-starting-at-boot?rq=1

https://unix.stackexchange.com/questions/98033/service-not-starting-at-boot

https://unix.stackexchange.com/questions/282174/squid-systemd-service-not-run-at-boot

https://unix.stackexchange.com/questions/255788/what-is-wrong-with-my-systemd-unit-file

https://unix.stackexchange.com/questions/137028/why-is-my-systemd-unit-loaded-but-inactive-dead

https://unix.stackexchange.com/questions/376038/systemd-why-is-this-service-inactive-dead/376043

https://unix.stackexchange.com/questions/159174/differences-between-inactive-vs-disabled-and-active-vs-enabled-services

https://unix.stackexchange.com/questions/376038/systemd-why-is-this-service-inactive-dead

https://unix.stackexchange.com/questions/251211/why-doesnt-my-systemd-user-unit-start-at-boot

还有很多。

我还想添加帮助我解决问题的调试步骤。

调试步骤:

第一步是尽量保持简单。

在后面添加media-blueray-Data.mountmedia-blueray-WDRed.mount

After=, Wants=, Requires=等等,使事情变得复杂。

对于用户服务,使用systemctl --user list-dependencies和/或systemd-analyze --user critical-chain找出您想要在部分WantedBy=中输入的内容[Install]-

笔记: [Install]如果您想要enable一项服务(在启动时运行),则需要部分。

$ systemctl --user list-dependencies 

default.target
● └─basic.target
●   ├─paths.target
●   ├─sockets.target
●   │ ├─dirmngr.socket
●   │ ├─gpg-agent-browser.socket
●   │ ├─gpg-agent-extra.socket
●   │ ├─gpg-agent-ssh.socket
●   │ └─gpg-agent.socket
●   └─timers.target

$ systemd-analyze --user critical-chain

default.target @12ms                                                                                                                                                                                                                                                           
└─basic.target @12ms                                                                                                                                                                                                                                                           
  └─paths.target @12ms    

如果需要,您可以使用带有Requires=&部分After=下的选定目标。[Unit]

使用启用服务systemctl --user enable lsyncd

如果重新启动计算机后,该服务未运行。使用以下命令检查服务状态systemctl --user status lsyncd

如果您发现类似以下错误lsyncd Start request repeated too quickly.

使用RestartSec=[Service]

笔记:

取无单位的秒值,或时间跨度值,如“5 分 20 秒”。默认为 100 毫秒。

您还可以使用类似

StartLimitInterval=10
StartLimitBurst=1

来解决这个问题。

在单元文件发生任何变化后使用

$ systemctl --user daemon-reload
// Disabling before enabling helped me. But I do not think it is necessary.
// $ systemctl --user disable lsyncd
$ systemctl --user enable lsyncd

附言:这是一个简短的调试指南。互联网上有很多关于此的文章。我的并不完整(还有十几个命令可以提供帮助,即journalctl使用不同的属性来读取日志)。但我希望它能帮助许多初学者使用 systemd。

相关内容