Sunday 5 February 2012

rTorrent & Screen Upstart 1.3 (Updated)

Upstart script for rTorrent for Upstart 1.3 (and older?)
Upstart 1.4 supports setting uid and gid so we can use that instead of start-stop-daemon or sudo or su to correctly track the process id using expect daemon and tell it to send the proper kill signal (for rTorrent is SIGINT)

So what do we do for Upstart 1.3?
Well, we're forced to use either start-stop-daemon or sudo or su to start a process as another user. Even with expect daemon it always tracks the wrong process id and in my case causes shutdown to freeze as it tries to kill a process that doesn't exist.

My solution is to start a job that cleanly kills (by sending a SIGINT) rTorrent when you'd expect it to. In my case runlevels [016]

#rtorrent-start.conf

description     "rTorrent - ncurses BitTorrent client based on LibTorrent"

start on (local-filesystems and net-device-up IFACE=eth0 and runlevel [2345])
stop on runlevel [016]

console none

exec start-stop-daemon --start -c <user> --exec /usr/bin/screen -- -dmUS rtorrent /usr/bin/rtorrent
##

#rtorrent-stop.conf

description "Correctly ensure rTorrent shuts down gracefully"

start on runlevel [016]

console none
task

script
 if [ -f /home/ <user> /.rtorrent/rtorrent.lock ]; then
   kill -s 2 $(cat /home/ <user> /.rtorrent/rtorrent.lock | awk -F+ '{ print $2 }')
   rm -f /home/ <user> /.rtorrent/rpc.socket
 fi
end script
##

.lock file cleaning should be left for rTorrent as it usually removes it when correctly killed. If an old .lock file remains its a clear indicator that rTorrent did not correctly shutdown.
Won't hurt to remove the socket file though (if you use it). Saves adding it in .rtorrent.rc

UPDATE:
I've recently had some issues with UDP trackers and DHT causing some torrents to not start downloading at all even when the swarm is healthy. I've since given up on rTorrent and have started using Deluge, which is nice and compact with a sweet web gui.

2 comments:

  1. I have yet to upgrade to 12.04 but I expect this to continue working however I suggest using the new stanzas in Upstart 1.4 like setguid and setuid as this would mean we can ditch start-stop-daemon and rtorrent-stop.conf as Upstart should be able to track and kill the correct PID.

    ReplyDelete