防止 systemd 计时器在按流量计费的连接上运行

Ger*_*kai 6 systemd systemd-timer

我有一个 systemd 计时器,每 10 分钟运行一次,用于下载我的电子邮件offlineimap。问题是我有时会收到大量邮件或大附件,同时我可能正在路上使用我的手机\xe2\x80\x99s 按流量计费的连接。

\n\n

现在我有一个自定义脚本,用于查询当前活动的连接nmcli并检查它是否是计量连接。offlineimap如果是这样,它根本不会运行。

\n\n

我的问题是,systemd 计时器可以做同样的事情吗?沿着这些思路:

\n\n
[Timer]\nOnUnitInactiveSec=10m\nRunOnMetered=no\n
Run Code Online (Sandbox Code Playgroud)\n

小智 4

You first need a Unit to depend upon which indicates whether the connection is or is not metered, like:

[Unit]
Description=Check if the current connection is metered

[Service]
Type=oneshot
ExecStart=check-metered-connection.sh
Run Code Online (Sandbox Code Playgroud)

The check-metered-connection.sh is a simple dbus check:

[Unit]
Description=Check if the current connection is metered

[Service]
Type=oneshot
ExecStart=check-metered-connection.sh
Run Code Online (Sandbox Code Playgroud)

Then your Units would depend on that service and you are good to go, like:

Requires=unmetered-connection.service
After=unmetered-connection.service
Run Code Online (Sandbox Code Playgroud)

Somebody already thought about that and was kind enough to publish the code on Github, see jdorel/systemd-metered-connection-dependency for the full code.

All code samples here are not mine but copied verbatim from the linked repository. Just copied here for completeness.