How to create launchd services for macOS

The following launchd script uses a separate user deluge, this is optional but recommended for security. To create a new deluge user follow the ​Apple help steps.

The paths to deluged and deluge-web assumes installation using Homebrew and will need modified if using other installation methods e.g. Deluge.app.

Daemon (deluged) service

Create the file /Library/LaunchDaemons/org.deluge-torrent.deluged.plist containing the following:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>org.deluge-torrent.deluged</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/deluged</string>
        <string>-d</string>
        <string>-L</string>
        <string>error</string>
        <string>-l</string>
        <string>/var/log/deluged.log</string>
    </array>
    <key>StandardOutPath</key>
    <string>/tmp/deluged.stdout</string>
    <key>StandardErrorPath</key>
    <string>/tmp/deluged.stderr</string>
    <!-- To enable running as 'deluge' user remove comments.
    <key>UserName</key>
    <string>deluge</string>
    -->
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
</dict>
</plist>

Set the service to load on startup and then start it:

sudo launchctl load -w /Library/LaunchDaemons/org.deluge-torrent.deluged.plist
sudo launchctl start org.deluge-torrent.deluged

Web UI (deluge-web) service

Create the file /Library/LaunchDaemons/org.deluge-torrent.deluge-web.plist containing the following:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>org.deluge-torrent.deluge-web</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/deluge-web</string>
        <string>-d</string>
        <string>-L</string>
        <string>error</string>
        <string>-l</string>
        <string>/var/log/deluge-web.log</string>
    </array>
    <key>StandardOutPath</key>
    <string>/tmp/deluge-web.stdout</string>
    <key>StandardErrorPath</key>
    <string>/tmp/deluge-web.stderr</string>
    <!-- To enable running as 'deluge' user remove comments.
    <key>UserName</key>
    <string>deluge</string>
    -->
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
</dict>
</plist>

Set the service to load on startup and then start it:

sudo launchctl load -w /Library/LaunchDaemons/org.deluge-torrent.deluge-web.plist
sudo launchctl start org.deluge-torrent.deluge-web

Logging

Logging is enabled by default in the above script, error level, and can be modified as required.