vortex-5 Posted September 21, 2011 Report Posted September 21, 2011 Ever wish you can run utorrent as a service?I.E. sudo service utorrent start sudo service utorrent stopsudo service utorrent statussudo service utorrent restartsudo service utorrent logI wanted the same thing so I wrote a script#!/bin/sh## uTorrent start stop service script## copy to /etc/init.d# run "update-rc.d utorrent defaults" to install# run "update-rc.d utorrent remove" to remove## @author FanFan Huang (kaboom05+utorrentscript@gmail.com)### BEGIN CONFIGURATION SECTION ##################################### Path where you want store the log file# (default recommended)LOGFILE=/var/log/utorrent.log# Path for the PID file where the process id is stored# (default is for debian and ubuntu systems)PIDFILE=/var/run/utorrent.pid# Path were you extracted utorrent serverUTORRENT_PATH=/home/vortex-5/shared/utorrent/utorrent-server-v3_0# Nice valueNICE=15# END CONFIGURATION SECTION #####################################uTorrentServer() {nice -n $NICE $UTORRENT_PATH/utserver -settingspath $UTORRENT_PATH -daemon -logfile $1 -pidfile $2}case "$1" in start)if [ -e "$PIDFILE" ]; thenecho "uTorrent is already running please terminate first"elseecho "uTorrent starting"uTorrentServer $LOGFILE $PIDFILEwhile [ ! -e $LOGFILE ]; dosleep 1 #Wait for file to be generateddonewhile [ "" = "$(cat $LOGFILE|grep 'IPv6 is installed')" ]; do#wait until utorrent has finished bootupecho -n ".$(cat $LOGFILE|grep 'IPv6 is installed')" sleep 1doneRESULT=$(cat $LOGFILE|grep 'bind failed') if [ -n "$RESULT" ]; thenecho "Port bind failure detected uTorrent may have limited functionality" elseecho "uTorrent started successfully" fifi;;stop)if [ -e "$PIDFILE" ]; thenecho -n "uTorrent is shutting down"PID=$(cat $PIDFILE)kill $PIDwhile [ -n "$(pidof "utserver")" ]; doecho -n "."sleep 1doneecho "uTorrent Service Terminated"rm $PIDFILErm $LOGFILEelseecho "Service is not currently running!"fi;;status)if [ -e "$PIDFILE" ]; thenPID=$(cat $PIDFILE)echo "uTorrent running pid: $PID"elseecho "uTorrent not running"fi;;restart)$0 stop$0 start;;log)if [ -e "$LOGFILE" ]; thenLOG=$(cat $LOGFILE)echo "$LOG"elseecho "uTorrent is not running no active log file"fi;;*)echo "Usage {start|stop|restart|status|log}"exitesacexit 0A direct link to the up to date version can be found here https://github.com/vortex-5/utorrent_initd/blob/master/utorrent (which I'll keep updated)This was originally written for Ubuntu but will work on debian and may work on other platforms as I'm using very standard bash commands.
mit Posted September 21, 2011 Report Posted September 21, 2011 Why nobody uses the search engine in this forum!? http://forum.utorrent.com/viewtopic.php?id=88044
vortex-5 Posted September 21, 2011 Author Report Posted September 21, 2011 The reference script doesn't do a few things 1. it doesn't make sure there was no port binding conflicts on startup (it doesn't read the log messages to make sure UTserver didn't freak out)2. It doesn't have a log command which I like 3. it doesn't wait long enough for the process to fully terminate when you stop it causing rebind to fail on next startupI've used the original author's format since he allows it to run as a user process and fixed those bugs#!/bin/sh### Original source: http://forum.utorrent.com/viewtopic.php?id=88044## uTorrent start stop service script## copy to /etc/init.d# run "update-rc.d utorrent defaults" to install# run "update-rc.d utorrent remove" to remove### version 2 improvments by:# @author FanFan Huang (kaboom05+utorrentscript@gmail.com)## #where you extracted your utserver executableUTORRENT_PATH=SOMEBASEDIR/utorrent-server-v3_0LOGFILE=/var/log/utorrent.log #must be a writable directoryUSER=utorrent #any user account you can create the utorrent user if you likeGROUP=usersNICE=15SCRIPTNAME=/etc/init.d/utorrent #must match this file nameDESC="uTorrent Server for Linux"CHDIR=$UTORRENT_PATHNAME=utserverUT_SETTINGS=$UTORRENT_PATHUT_LOG=$LOGFILEDAEMON_ARGS="-settingspath ${UT_SETTINGS} -logfile ${UT_LOG}"DAEMON=$CHDIR/$NAMEPIDFILE=/var/run/utorrent.pidSTOP_TIMEOUT=5INIT_VERBOSE=yesFAILURE=false# Exit if the package is not installed[ -x "$DAEMON" ] || 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.0-6) to ensure that this file is present.. /lib/lsb/init-functions## Function that starts the daemon/service#do_start(){ FAILURE=false # Return # 0 if daemon has been started # 1 if daemon was already running # 2 if daemon could not be started # 3 if port bind failed start-stop-daemon --start --nicelevel $NICE --quiet --make-pidfile --pidfile $PIDFILE --chuid $USER:$GROUP --chdir $CHDIR --background --exec $DAEMON --test > /dev/null if [ "$?" = "1" ]; then return 1 fi start-stop-daemon --start --nicelevel $NICE --quiet --make-pidfile --pidfile $PIDFILE --chuid $USER:$GROUP --chdir $CHDIR --background --exec $DAEMON -- $DAEMON_ARGS if [ "$?" != "0" ]; then return 2 fi #bind validation while [ ! -e $LOGFILE ]; do sleep 1 #Wait for file to be generated done while [ ! -n "$(cat $LOGFILE|grep 'IPv6 is installed')" ]; do #wait until utorrent has finished bootup (IPv6 MESSAGE is the last message) sleep 1 done RESULT=$(cat $LOGFILE|grep 'bind failed') if [ -n "$RESULT" ]; then return 3 fi return 0}## 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 start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME RETVAL="$?" if [ "$RETVAL" = 2 ]; then return 2 fi start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON RETVAL="$?" if [ "$RETVAL" = 2 ]; then return 2 fi #block process until server is completed shutting down fully while [ -n "$(pidof "$NAME")" ]; do sleep 1 done # Many daemons don't delete their pidfiles when they exit. rm -f $PIDFILE rm -f $LOGFILE #we don't want to keep our logfile return "$RETVAL"}## Function that sends a SIGHUP to the daemon/service#do_reload() { # # If the daemon can reload its configuration without # restarting (for example, when it is sent a SIGHUP), # then implement that here. # start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME return 0}msg_start() { case "$1" in 0|1) if [ "$VERBOSE" != no ]; then log_end_msg 0 fi ;; 2) if [ "$VERBOSE" != no ]; then log_end_msg 1 fi ;; 3) if [ "$VERBOSE" != no ]; then log_daemon_msg "Port bind failure detected uTorrent may have limited functionality please change the bind port and restart uTorrent" log_end_msg 1 fi ;; esac}msg_stop() { case "$1" in 0|1) if [ "$VERBOSE" != no ]; then log_end_msg 0 fi ;; *) if [ "$VERBOSE" != no ]; then log_daemon_msg "Failed to stop service exit status $STATUS" log_end_msg 1 fi esac}case "$1" in start) if [ "$VERBOSE" != no ]; then log_daemon_msg "Starting $DESC" fi do_start msg_start "$?" ;; stop) if [ "$VERBOSE" != no ]; then log_daemon_msg "Stopping $DESC" fi do_stop msg_stop "$?" ;; status) if [ -e "$PIDFILE" ]; then PID=" PID:($(cat $PIDFILE))" else PID="" fi status_of_proc "$DAEMON" "uTorrent$PID" if [ "$?" != "0" ]; then exit $? fi ;; restart|force-reload) # # If the "reload" option is implemented then remove the # 'force-reload' alias # log_daemon_msg "Restarting $DESC" do_stop STATUS="$?" if [ "$STATUS" -ne 0 ] && [ "$STATUS" -ne 1 ]; then log_daemon_msg "Could not stop exit status $STATUS" log_end_msg 1 exit 1 fi do_start STATUS="$?" case "$STATUS" in 0) log_end_msg 0 ;; *) log_daemon_msg "Restart failed start exist status $STATUS" log_end_msg 1 esac ;; log) if [ -e "$LOGFILE" ]; then LOG=$(cat $LOGFILE) echo "$LOG" else echo "uTorrent is not running no active log file" fi ;; *) echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload|log}" >&2 exit 3 ;;esacThe original URL still stands as it's in a versioning system: https://github.com/vortex-5/utorrent_initd/blob/master/utorrent
Recommended Posts
Archived
This topic is now archived and is closed to further replies.