Guest User

HTPC manager deamon

a guest
Oct 27th, 2012
139
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2. #
  3. # PROVIDE: HTPC manager
  4. #
  5. # Add the following lines to /etc/rc.conf.local or /etc/rc.conf or make a seperate script
  6. # to enable this service:
  7. #
  8. # HTPC_manager_enable (bool): Set to NO by default.
  9. # Set it to YES to enable it.
  10. # HTPC_manager_user: The user account HTPC manager daemon runs as what
  11. # you want it to be. It uses '_sabnzbd' user by
  12. # default. Do not sets it as empty or it will run
  13. # as root.
  14. # HTPC_manager_dir: Directory where HTPC manager lives.
  15. # Default: /usr/local/HTPC_manager
  16. # HTPC_manager_chdir: Change to this directory before running HTPC manager.
  17. # Default is same as HTPC_manager_dir.
  18. # HTPC_manager_pid: The name of the pidfile to create.
  19. # Default is htpc.pid in HTPC_manager_dir.
  20.  
  21. . /etc/rc.subr
  22.  
  23. name="HTPC_manager"
  24. rcvar=${name}_enable
  25.  
  26. load_rc_config ${name}
  27.  
  28. : ${HTPC_manager_enable:="YES"}
  29. : ${HTPC_manager_user:=""}
  30. : ${HTPC_manager_dir:="/mnt/data/programs/manager"}
  31. : ${HTPC_manager_chdir:="${HTPC_manager_dir}"}
  32. : ${HTPC_manager_pid:="${HTPC_manager_dir}/htpc.pid"}
  33. : ${HTPC_manager_conf:="${HTPC_manager_dir}/userdata/settings.conf"}
  34.  
  35. WGET="/usr/local/bin/wget" # You need wget for this script to safely shutdown CouchPotato.
  36. if [ -e "${HTPC_manager_conf}" ]; then
  37. HOST=`grep -A14 "\[core\]" "${HTPC_manager_conf}"|egrep "^host"|perl -wple 's/^host = (.*)$/$1/'`
  38. PORT=`grep -A14 "\[core\]" "${HTPC_manager_conf}"|egrep "^port"|perl -wple 's/^port = (.*)$/$1/'`
  39. CPAPI=`grep -A14 "\[core\]" "${HTPC_manager_conf}"|egrep "^api_key"|perl -wple 's/^api_key = (.*)$/$1/'`
  40. fi
  41.  
  42. status_cmd="${name}_status"
  43. stop_cmd="${name}_stop"
  44.  
  45. command="/usr/sbin/daemon"
  46. command_args="-f -p ${HTPC_manager_pid} python ${HTPC_manager_dir}/htpc.py ${HTPC_manager_flags}"
  47.  
  48. # Check for wget and refuse to start without it.
  49. if [ ! -x "${WGET}" ]; then
  50. warn "HTPC manager not started: You need wget to safely shut down HTPC manager."
  51. exit 1
  52. fi
  53.  
  54. # Ensure user is root when running this script.
  55. if [ `id -u` != "0" ]; then
  56. echo "Oops, you should be root before running this!"
  57. exit 1
  58. fi
  59.  
  60. verify_HTPC_manager_pid() {
  61. # Make sure the pid corresponds to the CouchPotato process.
  62. pid=`cat ${HTPC_manager_pid} 2>/dev/null`
  63. ps -p ${pid} | grep -q "python ${HTPC_manager_dir}/htpc.py"
  64. return $?
  65. }
  66.  
  67. # Try to stop HTPC manager cleanly by calling shutdown over http.
  68. couchpotato_stop() {
  69. if [ ! -e "${HTPC_manager_conf}" ]; then
  70. echo "HTPC manager settings file does not exist. Try starting HTPC manager, as this should create the file."
  71. exit 1
  72. fi
  73. echo "Stopping $name"
  74. verify_HTPC_manager_pid
  75. ${WGET} -O - -q "http://${HOST}:${PORT}/api/${CPAPI}/app.shutdown/" >/dev/null
  76. if [ -n "${pid}" ]; then
  77. wait_for_pids ${pid}
  78. echo "Stopped"
  79. fi
  80. }
  81.  
  82. HTPC_manager_status() {
  83. verify_HTPC_manager_pid && echo "$name is running as ${pid}" || echo "$name is not running"
  84. }
  85.  
  86. run_rc_command "$1"
RAW Paste Data