Skip to content
Newer
Older
100755 151 lines (123 sloc) 3.94 KB
81f1809 @Mar2zz better startup scripts
Mar2zz authored
1 #! /bin/sh
2
3 # Copyright (C) 2011- by Mar2zz <LaSi.Mar2zz@gmail.com>
4 # released under GPL, version 2 or later
5
6
7 ################################################
8 # #
9 # TO CONFIGURE EDIT /etc/default/couchpotato
10 # #
11 ################################################
12
13 ### BEGIN INIT INFO
14 # Provides: CouchPotato application instance
15 # Required-Start: $all
16 # Required-Stop: $all
17 # Default-Start: 2 3 4 5
18 # Default-Stop: 0 1 6
19 # Short-Description: starts instance of CouchPotato
20 # Description: starts instance of CouchPotato using start-stop-daemon
21 ### END INIT INFO
22
23
24 # main variables
25 DAEMON=/usr/bin/python
26 SETTINGS=/etc/default/couchpotato
27
28 SETTINGS_LOADED=FALSE
29
30 DESC=CouchPotato
31
32 # only accept values from /etc/default/couchpotato
562189b @Mar2zz Options to specify port in startup script
Mar2zz authored
33 unset RUN_AS CONFIG DATADIR PORT PID_FILE
81f1809 @Mar2zz better startup scripts
Mar2zz authored
34
35 . /lib/lsb/init-functions
36
37 [ -x $DAEMON ] || {
38 log_warning_msg "$DESC: Can't execute daemon, aborting. See $DAEMON";
39 return 1; }
40
41 [ -r $SETTINGS ] || {
42 log_warning_msg "$DESC: Can't read settings, aborting. See $SETTINGS";
43 return 1; }
44
45 check_retval() {
46 if [ $? -eq 0 ]; then
47 log_end_msg 0
48 return 0
49 else
50 log_end_msg 1
51 exit 1
52 fi
53 }
54
55 load_settings() {
56 if [ $SETTINGS_LOADED != "TRUE" ]; then
57 . $SETTINGS
58
59 [ -n "$APP_PATH" ] || {
60 log_warning_msg "$DESC: path to $DESC not set, aborting. See $SETTINGS";
61 return 1; }
62
63 [ $ENABLE_DAEMON != 1 ] && {
64 log_warning_msg "$DESC: daemon not enabled, aborting. See $SETTINGS";
65 return 1; }
66
67 [ -z "$RUN_AS" ] && {
68 log_warning_msg "$DESC: daemon username not set, aborting. See $SETTINGS";
69 return 1; }
70 [ -z "${RUN_AS%:*}" ] && exit 1
71
c4df280 @Mar2zz added --nolaunch option (don't launch browser, for daemons)
Mar2zz authored
72 DAEMON_OPTS="CouchPotato.py --quiet -d --nolaunch"
81f1809 @Mar2zz better startup scripts
Mar2zz authored
73 [ -n "$CONFIG" ] && DAEMON_OPTS="$DAEMON_OPTS --config=$CONFIG"
74 [ -z "$CONFIG" ] && DAEMON_OPTS="$DAEMON_OPTS --config=/home/$RUN_AS/.couchpotato/config.ini"
75 [ -n "$DATADIR" ] && DAEMON_OPTS="$DAEMON_OPTS --datadir=$DATADIR"
76 [ -z "$DATADIR" ] && DAEMON_OPTS="$DAEMON_OPTS --datadir=/home/$RUN_AS/.couchpotato"
562189b @Mar2zz Options to specify port in startup script
Mar2zz authored
77 [ -n "$PORT" ] && DAEMON_OPTS="$DAEMON_OPTS --port=$PORT"
81f1809 @Mar2zz better startup scripts
Mar2zz authored
78 if ! [ -n "$PID_FILE" ]; then
79 PID_FILE=/var/run/couchpotato/couchpotato.pid
80 fi
81 DAEMON_OPTS="$DAEMON_OPTS --pidfile=$PID_FILE"
82 SETTINGS_LOADED=TRUE
83 fi
84 return 0
85 }
86
87 load_settings || exit 0
88
89 is_running () {
90 # returns 1 when running, else 0.
91 PID=$(pgrep -f "$DAEMON_OPTS")
92 RET=$?
93 [ $RET -gt 1 ] && exit 1 || return $RET
94 }
95
96 handle_pid () {
97 PID_PATH=`dirname $PID_FILE`
98 [ -d $PID_PATH ] || mkdir -p $PID_PATH && chown -R $RUN_AS $PID_PATH > /dev/null || {
99 log_warning_msg "$DESC: Could not create $PID_FILE, aborting.";
100 return 1;}
101 }
102
103 enable_updates () {
104 chown -R $RUN_AS $APP_PATH > /dev/null || {
105 log_warning_msg "$DESC: $APP_PATH not writable for web-updates, See $SETTINGS";
106 return 0; }
107 }
108
109 start_couchpotato () {
110 if ! is_running; then
111 log_daemon_msg "Starting $DESC"
112 [ "$WEB_UPDATE" = 1 ] && enable_updates
113 handle_pid
114 start-stop-daemon -o -d $APP_PATH -c $RUN_AS --start --pidfile $PID_FILE --exec $DAEMON -- $DAEMON_OPTS
115 check_retval
116 else
117 log_success_msg "$DESC: already running (pid $PID)"
118 fi
119 }
120
121 stop_couchpotato () {
122 if is_running; then
123 log_daemon_msg "Stopping $DESC"
124 start-stop-daemon -o --stop --pidfile $PID_FILE --retry 15
125 check_retval
126 else
127 log_success_msg "$DESC: not running"
128 fi
129 }
130
131
132 case "$1" in
133 start)
134 start_couchpotato
135 ;;
136 stop)
137 stop_couchpotato
138 ;;
139 restart|force-reload)
140 stop_couchpotato
141 start_couchpotato
142 ;;
143 *)
144 N=/etc/init.d/$NAME
145 echo "Usage: $N {start|stop|restart|force-reload}" >&2
146 exit 1
147 ;;
148 esac
149
150 exit 0
Something went wrong with that request. Please try again.