Jinnmv
7/29/2015 - 7:14 PM

rtorrent as linux upstart service configuration

rtorrent as linux upstart service configuration

# PHP FPM configuration
# /etc/php5/fpm/pool.d/rutorrent.conf

[rutorrent]
user = rutorrent
group = rutorrent
listen = /var/run/php-fpm-rutorrent.sock
listen.owner = rutorrent
listen.group = www-data
listen.mode = 0660
pm = static
pm.max_children = 2
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /

env[PATH] = /usr/local/bin:/usr/bin:/bin
# Nginix rutorrent server configuration
# /etc/nginx/sites-available/rutorrent

# aliases
upstream backendrutorrent {
	server unix:/var/run/php-fpm-rutorrent.sock;
}
upstream backendrtorrent {
	server unix:/var/run/rtorrent/rpc.sock;
}

# rutorrent server
#
server {
	listen 80 default_server;
	#listen [::]:80 default_server;

	root /var/www/rutorrent;

	# Add index.php to the list if you are using PHP
	index index.php index.html index.htm index.nginx-debian.html;

	server_name rutorrent;

	location / {
		access_log /var/log/nginx/rutorrent.access.log;
		error_log /var/log/nginx/rutorrent.error.log;
		location ~ .php$ {
			fastcgi_split_path_info ^(.+\.php)(.*)$;
			fastcgi_pass    backendrutorrent;
			fastcgi_index   index.php;
			fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
			include fastcgi_params;
			fastcgi_intercept_errors        on;
			fastcgi_ignore_client_abort     off;
			fastcgi_connect_timeout         60;
			fastcgi_send_timeout            180;
			fastcgi_read_timeout            180;
			fastcgi_buffer_size             128k;
			fastcgi_buffers                 4       256k;
			fastcgi_busy_buffers_size       256k;
			fastcgi_temp_file_write_size    256k;
		}
	}

	location /RPC2 {
		access_log /var/log/nginx/rutorrent.rpc2.access.log;
		error_log /var/log/nginx/rutorrent.rpc2.error.log;
		include /etc/nginx/scgi_params;
		scgi_pass backendrtorrent;
	}

	#location ^~ /scgi {
	#	include scgi_params;
	#	scgi_pass unix:/home/rtorrent/.rtorrent/rpc.sock;
	#}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	#location ~ /\.ht {
	#	deny all;
	#}
}
#!/bin/sh
# /etc/init.d/rtorrent

### BEGIN INIT INFO
# Provides:          rtorrent
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts rtorrent as service
# Description:       starts rtorrent using start-stop-daemon and dtach
### END INIT INFO

# Author: Anton Savenko <anton.a.savenko@gmail.com>

# Do NOT "set -e"

# TODO: create folder. on status check process

## Settings
NAME=rtorrent
DESC="BitTorrent client for ncurses"
USER=rtorrent
RTORRENT=/usr/bin/rtorrent
DTACH=/usr/bin/dtach
RUNDIR=/var/run/$NAME
SESSIONDIR=/home/$USER/session
LOCK_FILE=$SESSIONDIR/rtorrent.lock
DTACH_SOCKET=$RUNDIR/dtach.sock
DAEMON="$DTACH -n $DTACH_SOCKET $RTORRENT"
SCRIPTNAME=/etc/init.d/$NAME
## End Settings

# Exit if the package is not installed
[ -x "/usr/bin/$NAME" ] || exit 0

# Read configuration variable file if it is present
#[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started

	if [ -S ${DTACH_SOCKET} ]; then
		log_warning_msg "rtorrent is already running"
		return 1
	fi
	
	if [ ! -d ${RUNDIR} ]; then
		if mkdir ${RUNDIR}; then
			chown ${USER}:${USER} ${RUNDIR}
		else
			log_warning_msg "unable to create a run folder ${RUNDIR}"
		fi
	fi

	if [ -f ${LOCK_FILE} ]; then

		PID=$(cat ${LOCK_FILE} | cut -d+ -f2)

		if ps -A | grep -sq "${PID}.*$(basename ${RTORRENT})"; then

			log_warning_msg "${NAME} has been started manually (pid ${PID})"
			log_daemon_msg "Stopping manually launched ${NAME}"

			if kill -s INT ${PID} > /dev/null 2>&1; then
				log_success_msg "done"
				sleep 1
			else
				log_error_msg "unable to stop ${NAME}"
				return 2
			fi
		else

			log_warrning_msg "${NAME} has crashed (${NAME} lock file exist ${LOCK_FILE} but no process running PID ${PID})"
			log_daemon_msg "removing lock file ${LOCK_FILE}"
			rm ${LOCK_FILE} && log_daemon_msg "done" || log_warning_msg "failed. trying to start ${NAME} anyway"

		fi
	fi

	log_daemon_msg "starting ${NAME}: "

	su - ${USER} -s /bin/sh -c "${DAEMON}" &&  return 0 || return 2	
}

#
# Function that stops the daemon/service
#
do_stop()
{
	# Return
	#   0 if daemon has been stopped
	#   1 if daemon was already stopped
	#   2 if daemon could not be stopped
	#   other if a failure occurred

	if [ -f ${SESSIONDIR}/rtorrent.lock ]; then

		PID=$(cat ${SESSIONDIR}/rtorrent.lock | cut -d+ -f2)

		if ps -A | grep -sq "${PID}.*$(basename ${RTORRENT})"; then

			log_daemon_msg "stopping ${NAME}"
			kill -s INT ${PID} > /dev/null 2>&1 && return 0 ||  return 2

		else

			log_warrning_msg "nothing to stop. ${NAME} has crashed (${NAME} lock file exist ${LOCK_FILE} but no process running PID ${PID})"
			log_progress_msg "removing lock file ${LOCK_FILE}"
			rm ${LOCK_FILE} && log_daemon_msg "ok" || log_daemon_msg "failed"
			return 1

		fi
	else
		log_warning_msg "rtorrent is not running"
		return 2
	fi
}

#
# Function shows daemon/service status
#
do_status()
{
	# Return
	#   0 if daemon is running
	#   1 if daemon is stopped
	#   2 if daemon has crashed

	if [ -S ${DTACH_SOCKET} ]; then
		log_success_msg "dtach is running (socket ${DTACH_SOCKET})"
	else
		log_warning_msg "dtach is not running"
	fi

	if [ -f ${SESSIONDIR}/rtorrent.lock ]; then
		PID=$(cat ${SESSIONDIR}/rtorrent.lock | cut -d+ -f2)
		if ps -A | grep -sq "${PID}.*$(basename ${RTORRENT})"; then
			log_success_msg "rtorrent is running (pid ${PID})"
		else
			log_failure_msg "rtorrent has crashed (rtorrent lock file exist ${SESSIONDIR}/rtorrent.lock but no process running PID ${PID})"
			return 2
		fi
		return 0
	else
		log_warning_msg "rtorrent is not running"
		return 2
	fi
}

do_connect()
{
	#log_daemon_msg "connecting to dtach"
	if [ -S ${DTACH_SOCKET} ]; then
		
		su - ${USER} -s /bin/sh -c "${DTACH} -a ${DTACH_SOCKET}" && return 0 || return 2
	else
		log_failure_msg "dtach session is not running"
		return 1
	fi
}


case "$1" in
	start)
		[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
		do_start
		case "$?" in
			0|1) log_progress_msg "done"; log_end_msg 0 ;;
			2)   log_end_msg 1 ;;
		esac
		;;


	stop)
		[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
		do_stop
		case "$?" in
			0|1) log_progress_msg "done"; log_end_msg 0 ;;
			2)   log_end_msg 1 ;;
		esac
		;;

	status)
		#[ "$VERBOSE" != no ] && log_daemon_msg "$NAME $DESC status:"
		do_status
		#case "$?" in
		#	0)   log_end_msg 0 ;;
		#	1|2) log_end_msg 1 ;;
		#esac
		;;
	connect)
		do_connect
		;;
	*)
		log_action_msg "* Usage: $SCRIPTNAME {start|stop|status|restart|force-reload|connect}" >&2
		exit 3
		;;
esac
# rtorrent configuration file
# /home/rtorrent/.rtorrent.rc

# Maximum and minimum number of peers to connect to per torrent.
#min_peers = 40
max_peers = 100

# Same as above but for seeding completed torrents (-1 = same as downloading)
min_peers_seed = -1
max_peers_seed = -1

# Maximum number of simultanious uploads per torrent.
max_uploads = 50

# Global upload and download rate in KiB. "0" for unlimited.
download_rate = 0
upload_rate = 0

# Default directory to save the downloaded torrents.
directory = "/media/Seagate Expansion Drive/Downloads"

# Default session directory. Make sure you don't run multiple instance
# of rtorrent using the same session directory. Perhaps using a
# relative path?
session = ~/.rtorrent/session

# Watch a directory for new torrents, and stop those that have been
# deleted.
schedule = watch_directory,5,5,load_start=~/.rtorrent/watch/*.torrent
schedule = untied_directory,5,5,stop_untied=
schedule = tied_directory,5,5,start_tied=

# Close torrents when diskspace is low. */
schedule = low_diskspace,5,60,close_low_diskspace=100M

# Stop torrents when reaching upload ratio in percent,
# when also reaching total upload in bytes, or when
# reaching final upload ratio in percent.
# example: stop at ratio 2.0 with at least 200 MB uploaded, or else ratio 20.0
#schedule = ratio,60,60,stop_on_ratio=200,200M,2000


# When the torrent finishes, it executes "mv -n <base_path> ~/Download/"
# and then sets the destination directory to "~/Download/". (0.7.7+)
# on_finished = move_complete,"execute=mv,-u,$d.get_base_path=,/home/downloads/<username>/complete/ ;d.set_directory=/home/downloads/<username>/complete/"

# The ip address reported to the tracker.
#ip = 127.0.0.1
#ip = rakshasa.no

# The ip address the listening socket and outgoing connections is
# bound to.
#bind = 127.0.0.1
#bind = rakshasa.no

# Port range to use for listening.
port_range = 55995-56000

# Start opening ports at a random position within the port range.
#port_random = yes

#scgi_port = 127.0.0.1:5000
scgi_local = /home/pi/.rtorrent/session/rpc.socket
schedule = chmod,0,0,"execute=chmod,777,/home/pi/.rtorrent/session/rpc.socket"

# Check hash for finished torrents. Might be usefull until the bug is
# fixed that causes lack of diskspace not to be properly reported.
#check_hash = no

# Set whetever the client should try to connect to UDP trackers.
#use_udp_trackers = no

# Alternative calls to bind and ip that should handle dynamic ip's.
#schedule = ip_tick,0,1800,ip=rakshasa
#schedule = bind_tick,0,1800,bind=rakshasa

# Encryption options, set to none (default) or any combination of the following:
# allow_incoming, try_outgoing, require, require_RC4, enable_retry, prefer_plaintext
#
# The example value allows incoming encrypted connections, starts unencrypted
# outgoing connections but retries with encryption if they fail, preferring
# plaintext to RC4 encryption after the encrypted handshake
#
encryption = allow_incoming,enable_retry,prefer_plaintext

# Enable DHT support for trackerless torrents or when all trackers are down.
# May be set to "disable" (completely disable DHT), "off" (do not start DHT),
# "auto" (start and stop DHT as needed), or "on" (start DHT immediately).
# The default is "off". For DHT to work, a session directory must be defined.
#
dht = auto
 
# UDP port to use for DHT.
#
dht_port = 6881
 
# Enable peer exchange (for torrents not marked private)
#
peer_exchange = yes

#
# Do not modify the following parameters unless you know what you're doing.
#

# Example of scheduling commands: Switch between two ip's every 5
# seconds.
#schedule = "ip_tick1,5,10,ip=torretta"
#schedule = "ip_tick2,10,10,ip=lampedusa"

# Remove a scheduled event.
#schedule_remove = "ip_tick1"