init scripts

This commit is contained in:
Ruud
2011-10-01 21:26:29 +02:00
parent a82b76cf32
commit 963c6d2ae0
4 changed files with 329 additions and 0 deletions
+90
View File
@@ -0,0 +1,90 @@
### BEGIN INIT INFO
# Provides: CouchPotato application instance
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts CouchPotato
# Description: starts CouchPotato
### END INIT INFO
# Source function library.
. /etc/init.d/functions
# Source couchpotato configuration
if [ -f /etc/sysconfig/couchpotato ]; then
. /etc/sysconfig/couchpotato
fi
prog=couchpotato
lockfile=/var/lock/subsys/$prog
## Edit user configuation in /etc/sysconfig/couchpotato to change
## the defaults
username=${CP_USER-couchpotato}
homedir=${CP_HOME-/opt/couchpotato}
datadir=${CP_DATA-~/.couchpotato}
pidfile=${CP_PIDFILE-/var/run/couchpotato/couchpotato.pid}
##
pidpath=`dirname ${pidfile}`
options=" --daemon --pidfile=${pidfile} --datadir=${datadir}"
# create PID directory if not exist and ensure the couchpotato user can write to it
if [ ! -d $pidpath ]; then
mkdir -p $pidpath
chown $username $pidpath
fi
if [ ! -d $datadir ]; then
mkdir -p $datadir
chown $username $datadir
fi
start() {
# Start daemon.
echo -n $"Starting $prog: "
daemon --user=${username} --pidfile=${pidfile} python ${homedir}/CouchPotato.py ${options}
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $lockfile
return $RETVAL
}
stop() {
echo -n $"Shutting down $prog: "
killproc -p ${pidfile} python
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $lockfile
return $RETVAL
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $prog
;;
restart|force-reload)
stop
start
;;
try-restart|condrestart)
if status $prog > /dev/null; then
stop
start
fi
;;
reload)
exit 3
;;
*)
echo $"Usage: $0 {start|stop|status|restart|try-restart|force-reload}"
exit 2
esac
+82
View File
@@ -0,0 +1,82 @@
#!/bin/sh
#
# PROVIDE: couchpotato
# REQUIRE: 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 CouchPotato 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 CouchPotato lives.
# Default: /usr/local/couchpotato
# couchpotato_chdir: Change to this directory before running CouchPotato.
# Default is same as couchpotato_dir.
# couchpotato_pid: The name of the pidfile to create.
# Default is couchpotato.pid in couchpotato_dir.
. /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 CouchPotato.
HOST="127.0.0.1" # Set CouchPotato address here.
PORT="8081" # Set CouchPotato port here.
CPAPI="" # Set CouchPotato API key
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 CouchPotato."
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 CouchPotato process.
pid=`cat ${couchpotato_pid} 2>/dev/null`
ps -p ${pid} | grep -q "python ${couchpotato_dir}/couchpotato.py"
return $?
}
# Try to stop CouchPotato cleanly by calling shutdown over http.
couchpotato_stop() {
echo "Stopping $name"
verify_couchpotato_pid
${WGET} -O - -q "http://${HOST}:${PORT}/${CPAPI}/app.shutdown/" >/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"
+94
View File
@@ -0,0 +1,94 @@
<?xml version="1.0"?>
<!DOCTYPE service_bundle SYSTEM "/usr/share/lib/xml/dtd/service_bundle.dtd.1">
<!--
Assumes user=couchpotato group=other
Assumes /opt/couchpotato is installation directory
See http://www.sun.com/bigadmin/content/selfheal/sdev_intro.jsp for more information
To install (see http://docs.sun.com/app/docs/doc/819-2379/fgour?l=en&a=view for more information)
svccfg import couchpotato.smf
To enable/disable
svcadm enable couchpotato
svcadm disable couchpotato
To check if failures
svcs -xv
To check logs
tail /var/svc/log/network-couchpotato\:default.log
-->
<service_bundle type='manifest' name='couchpotato'>
<service
name='network/couchpotato'
type='service'
version='1'>
<create_default_instance enabled='false' />
<single_instance />
<!--
Only start in muti-user mode
-->
<dependency name='multi-user'
grouping='require_all'
restart_on='none'
type='service'>
<service_fmri value='svc:/milestone/multi-user' />
</dependency>
<!--
Wait for network interfaces to be initialized.
-->
<dependency name='network'
grouping='require_all'
restart_on='none'
type='service'>
<service_fmri value='svc:/milestone/network:default'/>
</dependency>
<!--
Wait for all local filesystems to be mounted.
-->
<dependency name='filesystem-local'
grouping='require_all'
restart_on='none'
type='service'>
<service_fmri value='svc:/system/filesystem/local:default'/>
</dependency>
<!-- execute as user couchpotato -->
<method_context>
<method_credential user='couchpotato' group='other' />
</method_context>
<exec_method
type='method'
name='start'
exec='/opt/couchpotato/CouchPotato.py -d'
timeout_seconds='60'>
</exec_method>
<exec_method
type='method'
name='stop'
exec=':kill'
timeout_seconds='2'>
</exec_method>
<template>
<common_name>
<loctext xml:lang='C'>CouchPotato</loctext>
</common_name>
<documentation>
<doc_link name='couchpotato'
uri='http://www.couchpotatoapp.com/' />
</documentation>
</template>
</service>
</service_bundle>
+63
View File
@@ -0,0 +1,63 @@
#! /bin/sh
### BEGIN INIT INFO
# Provides: CouchPotato application instance
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts instance of CouchPotato
# Description: starts instance of CouchPotato using start-stop-daemon
### END INIT INFO
############### EDIT ME ##################
# path to app
APP_PATH=/usr/local/sbin/couchpotato
# path to python bin
DAEMON=/usr/bin/python
# startup args
DAEMON_OPTS=" CouchPotato.py -q"
# script name
NAME=couchpotato
# app name
DESC=CouchPotato
# user
RUN_AS=root
PID_FILE=/var/run/couchpotato.pid
############### END EDIT ME ##################
test -x $DAEMON || exit 0
set -e
case "$1" in
start)
echo "Starting $DESC"
start-stop-daemon -d $APP_PATH -c $RUN_AS --start --background --pidfile $PID_FILE --make-pidfile --exec $DAEMON -- $DAEMON_OPTS
;;
stop)
echo "Stopping $DESC"
start-stop-daemon --stop --pidfile $PID_FILE
;;
restart|force-reload)
echo "Restarting $DESC"
start-stop-daemon --stop --pidfile $PID_FILE
sleep 15
start-stop-daemon -d $APP_PATH -c $RUN_AS --start --background --pidfile $PID_FILE --make-pidfile --exec $DAEMON -- $DAEMON_OPTS
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0