首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ubuntu如何仅在VPN连接时启动服务,并在每次断开连接时重新启动服务

Ubuntu如何仅在VPN连接时启动服务,并在每次断开连接时重新启动服务
EN

Stack Overflow用户
提问于 2022-12-03 12:58:29
回答 1查看 18关注 0票数 0

当我获得连接到NordVPN后需要在引导时启动的服务时,我创建了一个测试服务器。

无论如何,我发现如果我也断开了连接,则需要在恢复到VPN的连接后重新启动服务。

你能帮我做这个吗?

非常感谢

我创建了一个服务,并延迟了启动的时间。

代码语言:javascript
复制
[Unit]
Description=qBittorrent-nox service
Documentation=man:qbittorrent-nox(1)
Wants=network-online.target
After=network-online.target nss-lookup.target

[Service]
ExecStartPre=/bin/sleep 60
# if you have systemd < 240 (Ubuntu 18.10 and earlier, for example), you probably want to use Type=>
Type=exec
# change user as needed
User=root
# The -d flag should not be used in this setup
ExecStart=/usr/bin/qbittorrent-nox
Restart=on-failure
RestartSec=1s
# uncomment this for versions of qBittorrent < 4.2.0 to set the maximum number of open files to unl>
#LimitNOFILE=infinity
# uncomment this to use "Network interface" and/or "Optional IP address to bind to" options
# without this binding will fail and qBittorrent's traffic will go through the default route
# AmbientCapabilities=CAP_NET_RAW

[Install]
WantedBy=multi-user.target
EN

回答 1

Stack Overflow用户

发布于 2022-12-04 02:58:44

Restart指令指定重新启动服务的条件。此指令的默认值为on-failure,这意味着只有当服务以非零退出代码退出时才会重新启动。在这种情况下,您可以修改此指令,以指定无论何时恢复VPN连接都应重新启动服务。

下面是如何在单元文件中修改重新启动指令的示例:

代码语言:javascript
复制
[Service]
...
Restart=on-failure-or-vpn-restored

使用此配置,无论何时使用非零退出代码退出服务,或在恢复VPN连接时,服务都将重新启动。

请注意,您需要创建一个独立的单元文件来管理VPN连接,并将此单元文件指定为服务的单元文件的依赖项。这将确保该服务仅在VPN连接建立后才启动。

例如,可以将以下行添加到服务的单元文件中,使其依赖于VPN连接:

代码语言:javascript
复制
[Unit]
...
After=vpn.service
Wants=vpn.service

这将确保您的服务只有在vpn.service单元成功启动之后才能启动。您将需要创建vpn.service单元文件,并指定管理VPN连接所需的详细信息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74666970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档