4

I am trying to get rtorrent to run as a systemd service, but the service wouldn't start. Here's the config file and any log I can get. Ask for more info if you need to. I am running:

$ 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'.

The config file..

[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

Some more logs:

$ 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'.

So far I have added the user rt to the adm group, but I can't figure it out why tmux can't be started as rt. I also authorized rt user to launch services thanks to the enable-linger option: loginctl enable-linger rt I first added the rt user with:sudo adduser --system --gecos "rTorrent Client" --disabled-password --group --home /home/rt rt. How to make rtorrent run as systemd service with tmuxas a dedicated user? Or is there any other way to run it as service with systemd? Any help is really appreciated.

UPDATE: So, just to get a fresh start, I have created a new user named rtorrent with: sudo adduser --system --gecos "rTorrent System Client" --disabled-password --group --home /home/rtorrent rtorrent and changed the /etc/systemd/system/rtorrent.service file to this (also adding system.daemon = true in /home/rtorrent/.rtorrent.rc, because of this post):

[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

But after all I get this error:

$ 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)

Why is this happening? What I am doing wrong?

UPDATE 2: One more thing, This post suggest not dropping any files in the /etc/systemd/system/, but instead, to drop them in /usr/local/lib/systemd/system which in Debian based systems is in /lib/systemd/system. Therefore, I moved the unit-file there and when enabling it, it automatically created a symlink to /etc/systemd/system/. But still,, I get this error:

$ 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
  • In OpenSuse, at least, the proper way to modify the system file involves using some variant of the following command. This leaves the original file in place rather than creating one in another system directory shadowing it: systemctl edit --full rtorrent.service Jun 23, 2021 at 6:23

3 Answers 3

5

rtorrent as a daemon and screen / tmux

Starting from rtorrent 0.9.7, screen / tmux is not required anymore to run rtorrent as a system-wide daemon if you don't need its console interface. It was previously mandatory since rtorrent required an active terminal to be controlled, even if you only used an external interface (e.g. ruTorrent) to manage your torrent via SCGI.

If you really want to run rtorrent using screen or tmux to be able to control it using a terminal, see this answer

Just add the following configuration to your rtorrent.rc file to run rtorrent as a daemon:

system.daemon.set = true

You will only be able to control it using an external software via SCGI, so you must also configure SCGI in the config file:

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

Note: your configuration file may already include a scgi_port = ... directive. It's the older way of opening the SCGI port and is equivalent to the above, but don't put both of them in your config file or rtorrent will complain about the port being already opened: Error in option file: <file>:<line>: SCGI already enabled..

Service file

The previous answer from Ljm Dullaart is correct about how the service file should look. But you must pay attention to the rtorrent config file in use.

By default, rtorrent will load the ~/.rtorrent.rc file as its config file. If you also load it using the -o import ~/.rtorrent.rc option in your unit file, it will make rtorrent load the file twice. It results about rtorrent complaining about the SCGI port already opened since it reads the network.scgi.open_port or scgi_port config lines twice. You should also add the -noption switch to prevent rtorrent from reading its default config file

The unit file /etc/systemd/system/rtorrent.service should look like:

[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
1
  • I will try it these days. You're probably right though.
    – techsk8
    Dec 6, 2021 at 20:56
5

I do not understand why anyone would want a tmux around your rtorrent. What is the function of the tmux here? You could try https://askubuntu.com/questions/802189/how-to-run-tmux-screen-with-systemd-230 if you absolutely want that extra process overhead.

Anyways, create a /etc/systemd/system/rtorrent.service with

[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

That should answer your last question.

8
  • "I do not understand why anyone would want a tmux around your rtorrent. What is the function of the tmux here?" - I am still a newbie when it comes about rtorrent, but this is how all the tutorials put it, the very few that exist..
    – techsk8
    May 27, 2021 at 6:42
  • 1
    I'm sorry, but tmux is a bit of a frustration for me. Keep in mind that tmux is a terminal multiplexer, not a generalized wrapper around things. In start-up scripts (or systemd-files), you should never need a tmux. May 27, 2021 at 7:11
  • Thanks, I will remember that. Also, look on the update on the post.
    – techsk8
    May 27, 2021 at 7:14
  • in your adduser, you gave /home/rt as home directory for the user rt. In the unit-file you use /home/rtorrent May 27, 2021 at 7:22
  • 1
    Could you try a rtorrent /usr/bin/rtorrent -o import=/home/rtorrent/.rtorrent.rc as the user rtorrent to verify that rtorrent works at all? May 27, 2021 at 7:33
0

This is what helped me:

  1. Install default config as described here. That page contains script that downloads and update config to use current username.

    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. Uncomment these 3 lines in config :

    system.daemon.set = true
    network.scgi.open_local = (cat,(session.path),rpc.socket)
    execute.nothrow = chmod,770,(cat,(session.path),rpc.socket)
    
  3. Start service as described in this answer, but do not specify -o parameter.

    ExecStart=/usr/bin/rtorrent
    

    rtorrent picks up config from ~/.rtorrent.rc automatically.

You will know if the config was loaded twice if you see lots of these errors in logs:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .