summaryrefslogtreecommitdiffstats
path: root/docs/source/how-to/systemd-service.md
blob: 6b785ad1534bbf7829bcc450fb247fd1d232b360 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# How to create systemd services for Linux

This guide walks you through setting up Deluge systemd services on Linux.

Ensure Deluge daemon `deluged` and Web UI `deluge-web` are installed. Use
`which` to check installation paths and if necessary modify the service
file `ExecStart` lines to point to alternative paths.

## Create a service specific user

For security it is best to run a service with a specific user and group.
You can create one using the following command:

```console
sudo adduser --system  --gecos "Deluge Service" --disabled-password --group --home /var/lib/deluge deluge
```

This creates a new system user and group named `deluge` with no login access
and home directory `/var/lib/deluge` which will be the default location for the
config files.

In addition you can add to the `deluge` group any users you wish to be able to
easily manage or access files downloaded by Deluge, for example:

```console
sudo adduser <username> deluge
```

## Daemon (deluged) service

Create the file `/etc/systemd/system/deluged.service` containing the following:

```eval_rst
.. literalinclude:: ../../../packaging/systemd/deluged.service
    :language: ini
```

### User configuration

To run the service using the previously created user e.g. `deluge`, first
create the service configuration directory:

```console
sudo mkdir /etc/systemd/system/deluged.service.d/
```

Then create a user file `/etc/systemd/system/deluged.service.d/user.conf` with
the following contents:

```eval_rst
.. literalinclude:: ../../../packaging/systemd/user.conf
    :language: ini
```

### Start deluged service

Now enable it to start up on boot, start the service and verify it is running:

```console
sudo systemctl enable /etc/systemd/system/deluged.service
sudo systemctl start deluged
sudo systemctl status deluged
```

### Umask for deluged downloaded files

The `umask` in the service file can be modified to determine access to files
downloaded by deluged (also applies to logging files). Some useful access
values are detailed as follows:

- `000` - full access for all users and groups.
- `007` - only user and group that `deluged` is running as (e.g. `deluge`)
  with no access from any other accounts.
- `002` - user and group `deluged` is running as with read-only for all other
  accounts.
- `022` - user `deluged` is running as with read-only for all other accounts.

The service for `deluged` must be stopped and started instead of just restarted
after changes.

## Web UI (deluge-web) service

Create the file `/etc/systemd/system/deluge-web.service` containing the following:

```eval_rst
.. literalinclude:: ../../../packaging/systemd/deluge-web.service
    :language: ini
```

### User configuration

To run the service using the previously created user e.g. `deluge`, first
create the service configuration directory:

```
sudo mkdir /etc/systemd/system/deluge-web.service.d/
```

Then create a user file `/etc/systemd/system/deluge-web.service.d/user.conf` with
the following contents:

```eval_rst
.. literalinclude:: ../../../packaging/systemd/user.conf
    :language: ini
```

### Start deluge-web service

Now enable it to start up on boot, start the service and verify it is running:

```console
sudo systemctl enable /etc/systemd/system/deluge-web.service
sudo systemctl start deluge-web
sudo systemctl status deluge-web
```

## Service logging

Create a log directory for Deluge and give the service user (e.g. `deluge`), full access:

```console
sudo mkdir -p /var/log/deluge
sudo chown -R deluge:deluge /var/log/deluge
sudo chmod -R 750 /var/log/deluge
```

The deluge log directory is now configured so that user `deluge` has full
access, group `deluge` read only and everyone else denied access. The `umask`
specified in the services sets the permission of new log files.

Enable logging in the service files by editing the `ExecStart` line, appending
`-l` and `-L` options:

```
ExecStart=/usr/bin/deluged -d -l /var/log/deluge/daemon.log -L warning
```

```
ExecStart=/usr/bin/deluge-web -d -l /var/log/deluge/web.log -L warning
```

See `deluged -h` for all available log-levels.

Restart the services:

```console
sudo systemctl daemon-reload
sudo systemctl restart deluged
sudo systemctl restart deluge-web
```

### Log rotation

To enable log rotation append `--logrotate` to the above `ExecStart` lines.

## Conditionally start deluged for mount points

If you have a USB disk drive or network drive that may not be immediately
available on boot or disconnected at random then you may want the `deluged`
service to wait for mount point to be ready before starting. If they are
unmounted or disconnected then `deluged` is stopped. When they become available
again `deluged` is started.

Ensure you have added the correct drive details to `fstab` or equivalent so
they are mounted at boot.

List the available drive mounts:

```console
sudo systemctl -t mount
```

Look for your mount point in the `Description` column. Mounts are formatted
similar to the mount point with `-`s replacing `/`s in the path.
e.g.: `media-xyz.mount`

Modify the `[Unit]` section of the `deluged.service` script by adding the
details below, substituting `xyz.mount` for the mount you want the service
to depend on:

```ini
[Unit]
Description=Deluge Bittorrent Client Daemon
# Start after network and specified mounts are available.
After=network-online.target xyz.mount
Requires=xyz.mount
# Stops deluged if mount points disconnect
BindsTo=xyz.mount
```

For multiple mount points add a space between additional entries. e.g.:

```init
After=network-online.target xyz.mount abc.mount def.mount
```

Modify the `[Install]` section to ensure the deluged service is started when
the mount point comes back online:

```ini
[Install]
WantedBy=multi-user.target xyz.mount
```

Reference: [systemd.unit](https://www.freedesktop.org/software/systemd/man/systemd.unit.html#RequiresMountsFor=)