CouchPotato on FreeBSD

For CouchPotato in FreeBSD, firstly acquaint yourself with my previous blog entry on Sabnzbd+Sickbeard on FreeBSD, and then perform the following additional steps:

At Step 3, fetch CouchPotato’s source code:

# cd /usr/local && git clone git://github.com/RuudBurger/CouchPotato.git couchpotato

Also, it is advisable to change the permission and ownership of the CouchPotato folder:

# chown root:_sabnzbd /usr/local/couchpotato

# chmod g+w /usr/local/couchpotato

At Step 4, add the following to /etc/rc.conf:

1
2
3

couchpotato_enable="YES"

.. and create /usr/local/etc/rc.d/couchpotato containing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

#!/bin/sh
#
# PROVIDE: couchpotato
# REQUIRE: DAEMON sabnzbd
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# couchpotato_enable (bool): Set to NO by default.
# Set it to YES to enable it.
# couchpotato_user: The user account Couch Potato daemon runs as what
# you want it to be. It uses '_sabnzbd' user by
# default. Do not sets it as empty or it will run
# as root.
# couchpotato_dir: Directory where Couch Potato lives.
# Default: /usr/local/couchpotato
# couchpotato_chdir: Change to this directory before running Couch Potato.
# Default is same as couchpotato_dir.
# couchpotato_pid: The name of the pidfile to create.
# Default is couchpotato.pid in couchpotato_dir.
PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"

. /etc/rc.subr

name="couchpotato"
rcvar=${name}_enable

load_rc_config ${name}

: ${couchpotato_enable:="NO"}
: ${couchpotato_user:="_sabnzbd"}
: ${couchpotato_dir:="/usr/local/couchpotato"}
: ${couchpotato_chdir:="${couchpotato_dir}"}
: ${couchpotato_pid:="${couchpotato_dir}/couchpotato.pid"}

WGET="/usr/local/bin/wget" # You need wget for this script to safely shutdown Couch Potato.
HOST="127.0.0.1" # Set Couch Potato address here.
PORT="5000" # Set Couch Potato port here.
SBUSR="" # Set Couch Potato username (if you use one) here.
SBPWD="" # Set Couch Potato password (if you use one) here.

status_cmd="${name}_status"
stop_cmd="${name}_stop"

command="/usr/sbin/daemon"
command_args="-f -p ${couchpotato_pid} python ${couchpotato_dir}/CouchPotato.py ${couchpotato_flags} --quiet"

# Check for wget and refuse to start without it.
if [ ! -x "${WGET}" ]; then
warn "Couchpotato not started: You need wget to safely shut down Couch Potato."
exit 1
fi

# Ensure user is root when running this script.
if [ `id -u` != "0" ]; then
echo "Oops, you should be root before running this!"
exit 1
fi

verify_couchpotato_pid() {
# Make sure the pid corresponds to the Couch Potato process.
pid=`cat ${couchpotato_pid} 2>/dev/null`
ps -p ${pid} | grep -q "python ${couchpotato_dir}/CouchPotato.py"
return $?
}

# Try to stop Couch Potato cleanly by calling shutdown over http.
couchpotato_stop() {
echo "Stopping $name"
verify_couchpotato_pid
${WGET} -O - -q --user=${SBUSR} --password=${SBPWD} "http://${HOST}:${PORT}/home/shutdown/?pid=${pid}" >/dev/null
if [ -n "${pid}" ]; then
wait_for_pids ${pid}
echo "Stopped"
fi
}

couchpotato_status() {
verify_couchpotato_pid && echo "$name is running as ${pid}" || echo "$name is not running"
}

run_rc_command "$1"