如何在專用用戶下將 rtorrent 作為 systemd 服務運行?

如何在專用用戶下將 rtorrent 作為 systemd 服務運行?

我試圖rtorrent以 運行systemd service,但服務無法啟動。這是設定檔和我可以獲得的任何日誌。如果需要,請詢問更多資訊。我在跑步:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.2 LTS
Release:        20.04
Codename:       focal
$ systemctl status rtorrent
● rtorrent.service - rTorrent
     Loaded: loaded (/etc/systemd/system/rtorrent.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Thu 2021-05-27 08:52:43 EEST; 5min ago
    Process: 20199 ExecStart=/usr/bin/tmux new-session -d -P -s rt -n rtorrent /usr/bin/rtorrent (code=exited, status=0/SUCCESS)
    Process: 20205 ExecStop=/usr/bin/tmux send-keys -t rt:rtorrent C-q (code=exited, status=1/FAILURE)
   Main PID: 20201 (code=exited, status=0/SUCCESS)

May 27 08:52:43 $MACHINE systemd[1]: Starting rTorrent...
May 27 08:52:43 $MACHINE tmux[20199]: rt:
May 27 08:52:43 $MACHINE systemd[1]: Started rTorrent.
May 27 08:52:43 $MACHINE tmux[20205]: no server running on /tmp/tmux-110/default
May 27 08:52:43 $MACHINE systemd[1]: rtorrent.service: Control process exited, code=exited, status=1/FAILURE
May 27 08:52:43 $MACHINE systemd[1]: rtorrent.service: Failed with result 'exit-code'.

設定檔..

[Unit]
Description=rTorrent
Requires=network.target local-fs.target

[Service]
Type=forking
KillMode=none
User=rt
Group=adm
ExecStart=/usr/bin/tmux new-session -d -P -s rt -n rtorrent /usr/bin/rtorrent
ExecStop=/usr/bin/tmux send-keys -t rt:rtorrent C-q
WorkingDirectory=/tmp/tmux-110/

[Install]
WantedBy=multi-user.target

更多日誌:

$ journalctl -u rtorrent
May 27 08:52:43 $MACHINE systemd[1]: Starting rTorrent...
May 27 08:52:43 $MACHINE tmux[20199]: rt:
May 27 08:52:43 $MACHINE systemd[1]: Started rTorrent.
May 27 08:52:43 $MACHINE tmux[20205]: no server running on /tmp/tmux-110/default
May 27 08:52:43 $MACHINE systemd[1]: rtorrent.service: Control process exited, code=exited, status=1/FAILURE
May 27 08:52:43 $MACHINE systemd[1]: rtorrent.service: Failed with result 'exit-code'.

到目前為止,我已將用戶添加rtadm群組中,但我不明白為什麼tmux不能以rt.rt由於啟用-linger選項,我還授權使用者啟動服務:loginctl enable-linger rt 我首先使用rt以下命令新增使用者:sudo adduser --system --gecos "rTorrent Client" --disabled-password --group --home /home/rt rt。如何以專用用戶身分rtorrent運行systemd服務?tmux或者還有其他方法可以將其作為服務運行嗎systemd?非常感謝任何幫助。

更新: 因此,為了重新開始,我創建了一個名為rtorrent:的新用戶sudo adduser --system --gecos "rTorrent System Client" --disabled-password --group --home /home/rtorrent rtorrent 並將/etc/systemd/system/rtorrent.service文件更改為此(還添加system.daemon = true/home/rtorrent/.rtorrent.rc因為這個帖子):

[Unit]
Description=rTorrent System Daemon
After=network.target

[Service]
Type=simple
User=rtorrent
Group=rtorrent

ExecStartPre=-/bin/rm -f /home/rtorrent/.session/rtorrent.lock
ExecStart=/usr/bin/rtorrent -o import=/home/rtorrent/.rtorrent.rc
Restart=on-failure
RestartSec=3

[Install]
WantedBy=multi-user.target

但畢竟我得到了這個錯誤:

$ systemctl status rtorrent
● rtorrent.service - rTorrent System Daemon
     Loaded: loaded (/etc/systemd/system/rtorrent.service; enabled; vendor preset: enabled)
     Active: activating (auto-restart) (Result: exit-code) since Thu 2021-05-27 10:12:26 EEST; 2s ago
    Process: 22855 ExecStartPre=/bin/rm -f /home/rtorrent/.session/rtorrent.lock (code=exited, status=0/SUCCESS)
    Process: 22856 ExecStart=/usr/bin/rtorrent -o import=/home/rtorrent/.rtorrent.rc (code=exited, status=255/EXCEPTION)
   Main PID: 22856 (code=exited, status=255/EXCEPTION)

為什麼會發生這種情況?我做錯了什麼?

更新2: 還有一件事,這個帖子建議不要將任何文件放在 中/etc/systemd/system/,而是將它們放在/usr/local/lib/systemd/system基於 Debian 的系統中的/lib/systemd/system.因此,我將 移到unit-file那裡,並在啟用它時,它自動創建了一個symlinkto /etc/systemd/system/。但是,我仍然收到此錯誤:

$ sudo systemctl status rtorrent
● rtorrent.service - rTorrent System Daemon
     Loaded: loaded (/lib/systemd/system/rtorrent.service; enabled; vendor preset: enabled)
     Active: activating (auto-restart) (Result: exit-code) since Thu 2021-05-27 10:39:14 EEST; 924ms ago
    Process: 24530 ExecStartPre=/bin/rm -f /home/rtorrent/.session/rtorrent.lock (code=exited, status=0/SUCCESS)
    Process: 24531 ExecStart=/usr/bin/rtorrent -o import=/home/rtorrent/.rtorrent.rc (code=exited, status=255/EXCEPTION)
   Main PID: 24531 (code=exited, status=255/EXCEPTION)

答案1

我不明白為什麼有人會想要一個tmux圍繞著你的rtorrent。這裡的作用是什麼tmux?你可以嘗試https://askubuntu.com/questions/802189/how-to-run-tmux-screen-with-systemd-230 如果你絕對想要額外的進程開銷。

無論如何,創建/etc/systemd/system/rtorrent.service一個

[Unit]
Description=rTorrent System Daemon
After=network.target

[Service]
Type=simple
User=rtorrent
Group=rtorrent

ExecStartPre=-/bin/rm -f /home/rt/.session/rtorrent.lock
ExecStart=/usr/bin/rtorrent -o import=/home/rt/rtorrent.rc
Restart=on-failure
RestartSec=3

[Install]
WantedBy=multi-user.target

這應該可以回答你的最後一個問題。

答案2

rtorrent 作為守護程式和 screen / tmux

從 rtorrent 0.9.7 開始,screen / tmux不再需要將 rtorrent 作為系統範圍的守護程式運行如果你不需要它的控制台介面。以前這是強制性的,因為 rtorrent 需要控制活動終端,即使您僅使用外部介面(例如 ruTorrent)透過 SCGI 管理您的 torrent。

如果您確實想使用 screen 或 tmux 運行 rtorrent 以便能夠使用終端控制它,請參閱這個答案

只需將以下配置新增至您的rtorrent.rc檔案即可將 rtorrent 作為守護程式執行:

system.daemon.set = true

只能使用外部軟體來控制它透過 SCGI,因此您也必須在設定檔中設定 SCGI:

# Bind SCGI to localhost only on port 5000
network.scgi.open_port = 127.0.0.1:5000

筆記:您的設定檔可能已經包含scgi_port = ...指令。這是開啟 SCGI 連接埠的舊方法,與上面的方法等效,但不要將它們都放在設定檔中,否則 rtorrent 會抱怨連接埠已開啟:Error in option file: <file>:<line>: SCGI already enabled.

服務檔案

Ljm Dullaart 先前的回答關於服務文件的外觀是正確的。但您必須注意使用中的 rtorrent 設定檔。

預設情況下,rtorrent 會載入該~/.rtorrent.rc檔案作為其設定檔。如果您也使用-o import ~/.rtorrent.rc單元檔案中的選項載入它,它將使 rtorrent 載入該文件兩次。它會導致 rtorrent 抱怨 SCGI 連接埠已打開,因為它讀取了network.scgi.open_portscgi_portconfig 行兩次。您還應該添加選項-n開關以防止 rtorrent 讀取其預設配置文件

單元文件/etc/systemd/system/rtorrent.service應如下所示:

[Unit]
Description=rTorrent system daemon
After=network.target

[Service]
Type=simple
User=torrent
Group=torrent

# Change these settings to match your install:
Environment=RTORRENT_DIR=/home/torrent
Environment=RTORRENT_CONFIG=${RTORRENT_DIR}/.rtorrent.rc
Environment=SESSION_DIR=${RTORRENT_DIR}/session

ExecStartPre=/bin/rm -f ${SESSION_DIR}/rtorrent.lock
ExecStart=/usr/bin/rtorrent -n -o import=${RTORRENT_CONFIG}

Restart=on-failure
RestartSec=3

[Install]
WantedBy=multi-user.target

答案3

這對我有幫助:

  1. 依照描述安裝預設配置這裡。此頁麵包含下載和更新配置以使用目前使用者名稱的腳本。

    curl -Ls "https://raw.githubusercontent.com/wiki/rakshasa/rtorrent/CONFIG-Template.md" \
        | sed -ne "/^######/,/^### END/p" \
        | sed -re "s:/home/USERNAME:$HOME:" >~/.rtorrent.rc
    mkdir -p ~/rtorrent/
    
  2. 在 config 中取消註解這 3 行:

    system.daemon.set = true
    network.scgi.open_local = (cat,(session.path),rpc.socket)
    execute.nothrow = chmod,770,(cat,(session.path),rpc.socket)
    
  3. 按照中所述啟動服務這個答案,但不指定-o參數。

    ExecStart=/usr/bin/rtorrent
    

    rtorrent自動從中取得配置~/.rtorrent.rc

如果您在日誌中看到大量這些錯誤,您就會知道配置是否載入了兩次:

1687310792 C Caught exception: 'Error in option file: ~/.rtorrent.rc:11: Invalid key.'.

相關內容