Ubuntu 服务中的 ExecStart 命令失败,但手动执行时有效

Ubuntu 服务中的 ExecStart 命令失败,但手动执行时有效

我正在尝试使用以下命令执行以下/usr/local/sbin/namespaced-openvpn --config /home/sb/vpn/static2.ovpn --writepid /var/run/openvpn-protected-foo-sb.pid --log /var/log/openvpn-protected-foo-sb.log --daemon操作:namespeced-openvpn

当我运行此命令并检查日志文件时,一切都正常运行

现在我想在 rtorrent 启动之前执行此命令,然后使用该隧道运行 rtorrent。我已从以下位置安装了 rtorrent瑞士军刀并且已为我创建了一个服务文件。我不是专家,但我尝试编辑此文件以实现此目的,并在/etc/systemd/system/[email protected]

[Unit]
Description=rTorrent
After=network.target

[Service]
Type=forking
KillMode=none
User=%i
ExecStartPre=-/bin/rm -f /home/sb/.sessions/rtorrent.lock
ExecStart=/usr/local/sbin/namespaced-openvpn --config /home/sb/vpn/static2.ovpn --writepid /var/run/openvpn-protected-foo-sb.pid --log /var/log/openvpn-protected-foo-sb.log --daemon && /usr/bin/screen -L -Logfile /home/sb/screen.log -S rtorrent -d -m bash -c "ip netns exec protected sudo -u sb /usr/bin/rtorrent"
ExecStop=/usr/bin/screen -X -S rtorrent quit && sudo pkill -F /var/run/openvpn-protected-foo-sb.pid
WorkingDirectory=/home/sb/

[Install]
WantedBy=multi-user.target

我运行了systemctl daemon-reload,停止/禁用了默认的 rtorrent@DEFAULTUSER,而是启用了 rtorrent@root,然后重新启动。重新启动后,运行结果systemctl status rtorrent@root显示:

[email protected] - rTorrent
     Loaded: loaded (/etc/systemd/system/[email protected]; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Fri 2021-09-03 18:11:54 UTC; 20min ago
    Process: 720 ExecStartPre=/bin/rm -f /home/sb/.sessions/rtorrent.lock (code=exited, status=0/SUCCESS)
    Process: 744 ExecStart=/usr/local/sbin/namespaced-openvpn --config /home/sb/vpn/static2.ovpn --writepid /var/run/openvpn-protected-foo-sb.pid --log /var/log/openvpn-protected-foo-sb.log --daemon && /u>

Sep 03 18:11:49 seedbox systemd[1]: Starting rTorrent...
Sep 03 18:11:54 seedbox systemd[1]: [email protected]: Control process exited, code=exited, status=1/FAILURE
Sep 03 18:11:54 seedbox systemd[1]: [email protected]: Failed with result 'exit-code'.
Sep 03 18:11:54 seedbox systemd[1]: Failed to start rTorrent.

来自 namespaced-openvpn 命令的日志文件cat /var/log/openvpn-protected-foo-sb.log

Options error: Unrecognized option or missing or extra parameter(s) in [CMD-LINE]:1: daemon (2.4.7)
Use --help for more information.

我是不是做错了什么?任何帮助我都会很感激

答案1

我发现您无法使用 &&,因为该命令不是在 shell 中执行的。为了使其工作,我可以执行类似这样的操作,ExecStart=/bin/bash -c 'COMMAND1 && COMMAND2'或者我可以使用多个ExecStartPre,因此文件现在如下所示:

[Unit]
Description=rTorrent
After=network.target

[Service]
Type=forking
KillMode=none
User=%i
ExecStartPre=/bin/rm -f /home/sb/.sessions/rtorrent.lock
ExecStartPre=/usr/local/sbin/namespaced-openvpn --config /home/sb/vpn/static2.ovpn --writepid /var/run/openvpn-protected-foo-sb.pid --log /var/log/openvpn-protected-foo-sb.log --daemon
ExecStart=/usr/bin/screen -L -Logfile /home/sb/screen.log -S rtorrent -d -m bash -c "ip netns exec protected sudo -u sb /usr/bin/rtorrent"
ExecStop=/usr/bin/screen -X -S rtorrent quit
ExecStop=pkill -F /var/run/openvpn-protected-foo-sb.pid
WorkingDirectory=/home/sb/

[Install]
WantedBy=multi-user.target

相关内容