我创建了一个目标文件 /etc/systemd/system/watch-for-sync-need-all.target
[Unit]
Description=systemd target to group services for all folders that create a sync need by changes
After=multi-user.target
Wants=watch-for-sync-need@_sl_home_sl_.service
Wants=watch-for-sync-need@_sl_stream_sl_.service
[Install]
Also=watch-for-sync-need@_sl_home_sl_.service
Also=watch-for-sync-need@_sl_stream_sl_.service
其目的是能够启动、停止、启用或禁用所有在目标指定的systemd模板服务/etc/systemd/system/[email protected]
[Unit]
Description=watch sync folders for changes then flag sync need and set rtcwake
BindsTo=watch-for-sync-need-all.target
After=watch-for-sync-need-all.target
[Service]
User=root
Group=root
Type=simple
ExecStart=/bin/bash /etc/custom/notify-on-change %i
Restart=on-failure
RestartSec=3
[Install]
WantedBy=watch-for-sync-need-all.target
如果它必须处理我的问题,我会发布 /etc/custom/notify-on-change 的调用脚本内容
#! /usr/bin/env bash
inotifywait -q -m -r -e modify,delete,create "${1//_sl_//}" | while read DIRECTORY EVENT FILE
do
echo "yes" > /etc/custom/log/sync-needed
bash /etc/custom/set-rtcwake
systemctl stop watch-for-sync-need-all.target
done
如果文件夹 /home/ 或 /stream/ 发生更改,inotifywait 会注意到,标记同步需求,在即将到来的晚上 3 点设置计算机自我唤醒并停止服务。(如果标记了同步需要,则机器上有一个 cronjob 会在 3 点钟后的几分钟内同步到另一台计算机。计算机在不使用时会自行关闭。这样,我可以在我的计算机上工作并制作/home/ 或 /stream/ 中的更改,然后并且只有在那时才会自动开始同步。)
我的问题是,我无法充分启用我的目标。可以毫无问题地启动或停止目标。这意味着,两个“子”单元都在运行。启用不会发出任何警告并在目录 /etc/systemd/system/watch-for-sync-need-all.target.wants 中创建相应的链接,但是当我的机器启动时,“子”单元没有运行。新启动后,我得到以下输出
systemctl status watch-for-sync-need-all.target
watch-for-sync-need-all.target - systemd target to group services for all folders that create a sync need by ch>
Loaded: loaded (/etc/systemd/system/watch-for-sync-need-all.target; indirect; vendor preset: enabled)
Active: inactive (dead)`enter code here`
或者
systemctl status watch-for-sync-need@_sl_home_sl.service
watch-for-sync-need@_sl_home_sl.service - watch sync folders for changes then flag sync need and set rtcwake
Loaded: loaded (/etc/systemd/system/[email protected]; disabled; vendor preset: enabled)
Active: inactive (dead)
如何让 systemd 在系统启动时启动目标(所有“子”单元)?