rtorrent DO or DIE
If you’re like me, you’re just a smidge OCD about your seedbox. You might even run one on your own home network. Yeah yeah, I know the risks. Blah blah. Anyways, I’ve been running my own on a mac mini with a 12TB external plugged into it, and I mostly use it headless. It’s been rocking awesome for five years now. However, I’ve sometimes had issues with rtorrent stability from time to time. That said, I finally got sick of it and decided to leverage the obscure dark arts of launchd. It’s not superbly documented, I can barely find much of use on the internet about it, and what little I can find doesn’t exactly shine a bright light on this black box tool.
So here it is. This is the crayola-smoking kludge I came up with, after much LLM assistance.
First, you’re going to need a shell script wrapper. We will name it rtorrent_wrapper.sh. Thus:
#!/bin/bash
# 1. Clear stuck crash locks
rm -f /Users/redlegion/.rtorrent.session/rtorrent.lock
# 2. Kill the isolated rtorrent tmux server if it's lingering
/opt/homebrew/bin/tmux -L rtorrent_service kill-server 2>/dev/null
# 3. Start a private tmux server and run rtorrent
/opt/homebrew/bin/tmux -L rtorrent_service new-session -d -s rtorrent 'ulimit -n 4096 && /opt/homebrew/bin/rtorrent'
# 4. Loop using the private socket to keep launchd engaged
while /opt/homebrew/bin/tmux -L rtorrent_service has-session -t rtorrent 2>/dev/null; do
sleep 3
done
# 5. Force an error code so launchd triggers a mandatory restart
exit 1
So we
- purge any hanging lockfile
- kill any previous session if it’s still there
- kick off tmux with fat file limits in a session we won’t be able to access anyway named “rtorrent”
- obsessively check if session is up every three seconds
- gotta toss a error code to let launchd know what’s up
That provides a pretty shoddy garbage kludge to work from temporarily (HA, this shit is staying installed forever -ED) until we develop a more intelligent solution.
Next up, a plist. We’ll name it org.freedesktop.rtorrent.plist.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://apple.com">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.freedesktop.rtorrent</string>
<key>ProgramArguments</key>
<array>
<string>/Users/redlegion/rtorrent_wrapper.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
</dict>
<key>WorkingDirectory</key>
<string>/Users/redlegion</string>
<key>SoftResourceLimits</key>
<dict>
<key>NumberOfFiles</key>
<integer>4096</integer>
</dict>
<key>HardResourceLimits</key>
<dict>
<key>NumberOfFiles</key>
<integer>4096</integer>
</dict>
<key>StandardOutPath</key>
<string>/tmp/rtorrent.stdout.log</string>
<key>StandardErrorPath</key>
<string>/tmp/rtorrent.stderr.log</string>
</dict>
</plist>
So a few things here. One, it runs the wrapper we made earlier. Lazy, I know. Very lazy. Whatever. Does it look like I care? So we want a few things to happen here. One, we want to make sure our shit is always running. Set RunAtLoad and KeepAlive. We also doubled up on the resource limits because I took 14 whacks at this and never asked it to optimize at the end. Mostly because I knew it wouldn’t work for shit. Listen, don’t use this as it is. Instead, just be inspired to make me look bad and do it better. Okay?
So ANYWAYS.
We have logging happening. Haven’t checked em, don’t know if it’s there or just blank lines. Could be filling my /tmp with gigs of SPACE characters. Who fucking cares?
In order to make this shitpile start to chooch you gotta poke it. This is how you poke it: launchctl bootstrap gui/501 ~/Library/LaunchAgents/org.freedesktop.rtorrent.plist. That’s it. It’ll start and good fucking luck ever stopping it. It’s impossible. I know.
So I’m sure you’re thinking “What good is the thing if you can’t touch the result of it?” You’d be fuckin right. That’s why you set up flood on the back end. You’ll have to add it to your .rtorrent.rc and then npm install flood and I’m not going into further detail on that. I would, normally, but the edibles are kicking in and I’m just fucking not gonna.
Besides, you’re a growed up, you can figure that shit out on your own. So after we poke our rolled plist and wrapper we should see an impossible to kill tmux/rtorrent. I know. I’ve tried. So how to make it die? launchctl bootout gui/501 org.freedesktop.rtorrent. I’m pretty sure. I dunno. Google it.
So, yeah. This is some Tony Stark ‘in a cave’ type bullshit that looks every bit as shoddy and piss-poor welded monstrosity.
But. it. fucking. works.