Advertisement
CGar

Modified Dual Boot Deluge Init Sctipt

Jan 21st, 2015
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.18 KB | None | 0 0
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides:          deluge-daemon
  4. # Required-Start:    $local_fs $remote_fs
  5. # Required-Stop:     $local_fs $remote_fs
  6. # Should-Start:      $network
  7. # Should-Stop:       $network
  8. # Default-Start:     2 3 4 5
  9. # Default-Stop:      0 1 6
  10. # Short-Description: Daemonized version of deluge and webui.
  11. # Description:       Starts the deluge daemon with the user specified in
  12. #                    /etc/default/deluge-daemon.
  13. ### END INIT INFO
  14.  
  15. # Author: Adolfo R. Brandes
  16. # Updated by: Jean-Philippe "Orax" Roemer
  17.  
  18. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  19. DESC="Deluge Daemon"
  20. NAME1="deluged"
  21. NAME2="deluge"
  22. DAEMON1=/usr/bin/deluged
  23. DAEMON1_ARGS="-d"             # Consult `man deluged` for more options
  24. DAEMON2=/usr/bin/deluge-web
  25. DAEMON2_ARGS=""               # Consult `man deluge-web` for more options
  26. PIDFILE1=/var/run/$NAME1.pid
  27. PIDFILE2=/var/run/$NAME2.pid
  28. UMASK=022                     # Change this to 0 if running deluged as its own user
  29. PKGNAME=deluged
  30. SCRIPTNAME=/etc/init.d/$PKGNAME
  31.  
  32. # Exit if the package is not installed
  33. [ -x "$DAEMON1" -a -x "$DAEMON2" ] || exit 0
  34.  
  35. # Read configuration variable file if it is present
  36. [ -r /etc/default/$PKGNAME ] && . /etc/default/$PKGNAME
  37.  
  38. # Load the VERBOSE setting and other rcS variables
  39. [ -f /etc/default/rcS ] && . /etc/default/rcS
  40.  
  41. # Define LSB log_* functions.
  42. # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
  43. . /lib/lsb/init-functions
  44.  
  45. if [ -z "$RUN_AT_STARTUP" -o "$RUN_AT_STARTUP" != "YES" ]
  46. then
  47.    log_warning_msg "Not starting $PKGNAME, edit /etc/default/$PKGNAME to start it."
  48.    exit 0
  49. fi
  50.  
  51. if [ -z "$DELUGED_USER" ]
  52. then
  53.     log_warning_msg "Not starting $PKGNAME, DELUGED_USER not set in /etc/default/$PKGNAME."
  54.     exit 0
  55. fi
  56.  
  57. #
  58. # Function to switch directory name formats from windows to linux
  59. #
  60. fix_paths()
  61. {
  62.    # `/home/cgar/.config/deluge/state` must be symbolic link to the windows state folder.
  63.    stateFilePath=/home/cgar/.config/deluge/state/torrents.state
  64.  
  65.    #Replace S:\\Downloads with /mnt/dataDrive/Downloads
  66.    sed -i 's#S:\\\\Downloads#/mnt/dataDrive/Downloads#g' $stateFilePath
  67.  
  68.    #Replace C:\\Users\\wCGar\\Downloads with /mnt/dataDrive/Downloads
  69.    #probably not needed. But damn you deluge! You shoulden't even be thinking about my SSD! =P
  70.    sed -i 's#C:\\\\Users\\\\wCGar\\\\Downloads#/mnt/dataDrive/Downloads#g' $stateFilePath
  71. }
  72. #
  73. # Function to verify if a pid is alive
  74. #
  75. is_alive()
  76. {
  77.    pid=`cat $1` > /dev/null 2>&1
  78.    kill -0 $pid > /dev/null 2>&1
  79.    return $?
  80. }
  81.  
  82. #
  83. # Function that starts the daemon/service
  84. #
  85. do_start()
  86. {
  87.    # Return
  88.    #   0 if daemon has been started
  89.    #   1 if daemon was already running
  90.    #   2 if daemon could not be started
  91.  
  92.    is_alive $PIDFILE1
  93.    RETVAL1="$?"
  94.  
  95.    if [ $RETVAL1 != 0 ]; then
  96.        rm -f $PIDFILE1
  97.        fix_paths # Change windows paths to linux paths before starting.
  98.        start-stop-daemon --start --background --quiet --pidfile $PIDFILE1 --make-pidfile \
  99.        --exec $DAEMON1 --chuid $DELUGED_USER --user $DELUGED_USER --umask $UMASK -- $DAEMON1_ARGS
  100.        RETVAL1="$?"
  101.    else
  102.        is_alive $PIDFILE2
  103.        RETVAL2="$?"
  104.        [ "$RETVAL2" = "0" -a "$RETVAL1" = "0" ] && return 1
  105.    fi
  106.  
  107.    is_alive $PIDFILE2
  108.    RETVAL2="$?"
  109.  
  110.    if [ $RETVAL2 != 0 ]; then
  111.         sleep 2
  112.         rm -f $PIDFILE2
  113.         start-stop-daemon --start --background --quiet --pidfile $PIDFILE2 --make-pidfile \
  114.         --exec $DAEMON2 --chuid $DELUGED_USER --user $DELUGED_USER --umask $UMASK -- $DAEMON2_ARGS
  115.         RETVAL2="$?"
  116.    fi
  117.    [ "$RETVAL1" = "0" -a "$RETVAL2" = "0" ] || return 2
  118. }
  119.  
  120. #
  121. # Function that stops the daemon/service
  122. #
  123. do_stop()
  124. {
  125.    # Return
  126.    #   0 if daemon has been stopped
  127.    #   1 if daemon was already stopped
  128.    #   2 if daemon could not be stopped
  129.    #   other if a failure occurred
  130.  
  131.    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user $DELUGED_USER --pidfile $PIDFILE2
  132.    RETVAL2="$?"
  133.    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user $DELUGED_USER --pidfile $PIDFILE1
  134.    RETVAL1="$?"
  135.    [ "$RETVAL1" = "2" -o "$RETVAL2" = "2" ] && return 2
  136.  
  137.    rm -f $PIDFILE1 $PIDFILE2
  138.  
  139.    [ "$RETVAL1" = "0" -a "$RETVAL2" = "0" ] && return 0 || return 1
  140. }
  141.  
  142. case "$1" in
  143.   start)
  144.    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME1"
  145.    do_start
  146.    case "$?" in
  147.       0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  148.       2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  149.    esac
  150.    ;;
  151.   stop)
  152.    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME1"
  153.    do_stop
  154.    case "$?" in
  155.       0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  156.       2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  157.    esac
  158.    ;;
  159.   restart|force-reload)
  160.    log_daemon_msg "Restarting $DESC" "$NAME1"
  161.    do_stop
  162.    case "$?" in
  163.      0|1)
  164.       do_start
  165.       case "$?" in
  166.          0) log_end_msg 0 ;;
  167.          1) log_end_msg 1 ;; # Old process is still running
  168.          *) log_end_msg 1 ;; # Failed to start
  169.       esac
  170.       ;;
  171.      *)
  172.         # Failed to stop
  173.       log_end_msg 1
  174.       ;;
  175.    esac
  176.    ;;
  177.   *)
  178.    echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
  179.    exit 3
  180.    ;;
  181. esac
  182.  
  183. :
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement