Advertisement
Guest User

/etc/init.d/sickbeard

a guest
Jun 26th, 2010
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.36 KB | None | 0 0
  1. #!/bin/bash
  2. #############
  3. ###<Notes>###
  4. #############
  5. # This script depends on screen.
  6. # For the stop function to work, you must set an
  7. # explicit session directory using absolute paths (no, ~ is not absolute) in your sickbeard.rc.
  8. # If you typically just start sickbeard with just "sickbeard" on the
  9. # command line, all you need to change is the "user" option.
  10. # Attach to the screen session as your user with
  11. # "screen -dr sickbeard". Change "sickbeard" with srnname option.
  12. # Licensed under the GPLv2 by lostnihilist: lostnihilist _at_ gmail _dot_ com
  13. ##############
  14. ###</Notes>###
  15. ##############
  16.  
  17. #######################
  18. ##Start Configuration##
  19. #######################
  20. # You can specify your configuration in a different file
  21. # (so that it is saved with upgrades, saved in your home directory,
  22. # or whatever reason you want to)
  23. # by commenting out/deleting the configuration lines and placing them
  24. # in a text file (say /home/user/.sickbeard.init.conf) exactly as you would
  25. # have written them here (you can leave the comments if you desire
  26. # and then uncommenting the following line correcting the path/filename
  27. # for the one you used. note the space after the ".".
  28. # . /etc/sickbeard.init.conf
  29.  
  30.  
  31. #Do not put a space on either side of the equal signs e.g.
  32. # user = user
  33. # will not work
  34. # system user to run as (can only use one)
  35. user="CHANGEME"
  36.  
  37. # system user to run as # not implemented, see d_start for beginning implementation
  38. # group=$(id -ng "$user")
  39.  
  40. # set of options to run with each instance, separated by a new line
  41. # must keep parentheses around the entire statement
  42. #if no special options, specify with: ""
  43. options=("")
  44. # Examples:
  45. # starts one instance, sourcing both .sickbeard.rc and .sickbeard2.rc
  46. # options=("-o import=~/.sickbeard2.rc")
  47. # starts two instances, ignoring .sickbeard.rc for both, and using
  48. # .sickbeard2.rc for the first, and .sickbeard3.rc for the second
  49. # we do not check for valid options
  50. # options=("-n -o import=~/.sickbeard2.rc" "-n -o import=~/sickbeard3.rc")
  51.  
  52. # default directory for screen, needs to be an absolute path
  53. base=$(su -c 'echo $HOME' $user)
  54.  
  55. # name of screen session
  56. srnname="sickbeard"
  57.  
  58. # file to log to (makes for easier debugging if something goes wrong)
  59. logfile="/var/log/SickBeard.log"
  60. #######################
  61. ###END CONFIGURATION###
  62. #######################
  63.  
  64. PATH=/usr/bin:/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin
  65. DESC="sickbeard"
  66. NAME=sickbeard
  67. DAEMON=$NAME
  68. SCRIPTNAME=/etc/init.d/$NAME
  69.  
  70. d_start() {
  71.   [ -d "${base}" ] && cd "${base}"
  72.   stty stop undef && stty start undef
  73.   su -c "screen -S "${srnname}" -X screen sickbeard ${options} 2>&1 1>/dev/null" ${user} | tee -a "$logfile" >&2
  74.   # this works for the screen command, but starting sickbeard below adopts screen session gid
  75.   # even if it is not the screen session we started (e.g. running under an undesirable gid
  76.   #su -c "screen -ls | grep -sq "\.${srnname}[[:space:]]" " ${user} || su -c "sg \"$group\" -c \"screen -fn -dm -S ${srnname} 2>&1 1>/dev/null\"" ${user} | tee -a "$logfile" >&2
  77.   for (( i=0 ; i < ${#options[@]} ; i++ )) ;  do
  78.     sleep 3
  79.     su -c "screen -dmS ${srnname} sickbeard ${options[i]} 2>&1 1>/dev/null" ${user} | tee -a "$logfile" >&2
  80.     #su -c "screen -S "${srnname}" -X screen sickbeard ${options[i]} 2>&1 1>/dev/null" ${user} | tee -a "$logfile" >&2
  81.   done
  82. }
  83.  
  84. d_stop() {
  85.   for (( i=0 ; i < ${#config[@]} ; i++ )) ; do
  86.     session=$(getsession "${config[i]}")
  87.     if ! [ -s ${session}/sickbeard.lock ] ; then
  88.         return
  89.     fi
  90.     pid=$(cat ${session}/sickbeard.lock | awk -F: '{print($2)}' | sed "s/[^0-9]//g")
  91.     # make sure the pid doesn't belong to another process
  92.     if ps -A | grep -sq ${pid}.*sickbeard ; then
  93.         kill -s INT ${pid}
  94.     fi
  95.   done
  96. }
  97.  
  98. getsession() {
  99.     session=$(cat "$1" | grep "^[[:space:]]*session[[:space:]]*=" | sed "s/^[[:space:]]*session[[:space:]]*=[[:space:]]*//" )
  100.     #session=${session/#~/`getent passwd ${user}|cut -d: -f6`}
  101.     echo $session
  102. }
  103.  
  104. case "$1" in
  105.   start)
  106.     echo -n "Starting $DESC: $NAME"
  107.     d_start
  108.     echo "."
  109.     ;;
  110.   stop)
  111.     echo -n "Stopping $DESC: $NAME"
  112.     d_stop
  113.     echo "."
  114.     ;;
  115.   restart|force-reload)
  116.     echo -n "Restarting $DESC: $NAME"
  117.     d_stop
  118.     sleep 1
  119.     d_start
  120.     echo "."
  121.     ;;
  122.   *)
  123.     echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
  124.     exit 1
  125.     ;;
  126. esac
  127.  
  128. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement