Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #############
- ###<Notes>###
- #############
- # This script depends on screen.
- # For the stop function to work, you must set an
- # explicit session directory using absolute paths (no, ~ is not absolute) in your sickbeard.rc.
- # If you typically just start sickbeard with just "sickbeard" on the
- # command line, all you need to change is the "user" option.
- # Attach to the screen session as your user with
- # "screen -dr sickbeard". Change "sickbeard" with srnname option.
- # Licensed under the GPLv2 by lostnihilist: lostnihilist _at_ gmail _dot_ com
- ##############
- ###</Notes>###
- ##############
- #######################
- ##Start Configuration##
- #######################
- # You can specify your configuration in a different file
- # (so that it is saved with upgrades, saved in your home directory,
- # or whatever reason you want to)
- # by commenting out/deleting the configuration lines and placing them
- # in a text file (say /home/user/.sickbeard.init.conf) exactly as you would
- # have written them here (you can leave the comments if you desire
- # and then uncommenting the following line correcting the path/filename
- # for the one you used. note the space after the ".".
- # . /etc/sickbeard.init.conf
- #Do not put a space on either side of the equal signs e.g.
- # user = user
- # will not work
- # system user to run as (can only use one)
- user="CHANGEME"
- # system user to run as # not implemented, see d_start for beginning implementation
- # group=$(id -ng "$user")
- # set of options to run with each instance, separated by a new line
- # must keep parentheses around the entire statement
- #if no special options, specify with: ""
- options=("")
- # Examples:
- # starts one instance, sourcing both .sickbeard.rc and .sickbeard2.rc
- # options=("-o import=~/.sickbeard2.rc")
- # starts two instances, ignoring .sickbeard.rc for both, and using
- # .sickbeard2.rc for the first, and .sickbeard3.rc for the second
- # we do not check for valid options
- # options=("-n -o import=~/.sickbeard2.rc" "-n -o import=~/sickbeard3.rc")
- # default directory for screen, needs to be an absolute path
- base=$(su -c 'echo $HOME' $user)
- # name of screen session
- srnname="sickbeard"
- # file to log to (makes for easier debugging if something goes wrong)
- logfile="/var/log/SickBeard.log"
- #######################
- ###END CONFIGURATION###
- #######################
- PATH=/usr/bin:/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin
- DESC="sickbeard"
- NAME=sickbeard
- DAEMON=$NAME
- SCRIPTNAME=/etc/init.d/$NAME
- d_start() {
- [ -d "${base}" ] && cd "${base}"
- stty stop undef && stty start undef
- su -c "screen -S "${srnname}" -X screen sickbeard ${options} 2>&1 1>/dev/null" ${user} | tee -a "$logfile" >&2
- # this works for the screen command, but starting sickbeard below adopts screen session gid
- # even if it is not the screen session we started (e.g. running under an undesirable gid
- #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
- for (( i=0 ; i < ${#options[@]} ; i++ )) ; do
- sleep 3
- su -c "screen -dmS ${srnname} sickbeard ${options[i]} 2>&1 1>/dev/null" ${user} | tee -a "$logfile" >&2
- #su -c "screen -S "${srnname}" -X screen sickbeard ${options[i]} 2>&1 1>/dev/null" ${user} | tee -a "$logfile" >&2
- done
- }
- d_stop() {
- for (( i=0 ; i < ${#config[@]} ; i++ )) ; do
- session=$(getsession "${config[i]}")
- if ! [ -s ${session}/sickbeard.lock ] ; then
- return
- fi
- pid=$(cat ${session}/sickbeard.lock | awk -F: '{print($2)}' | sed "s/[^0-9]//g")
- # make sure the pid doesn't belong to another process
- if ps -A | grep -sq ${pid}.*sickbeard ; then
- kill -s INT ${pid}
- fi
- done
- }
- getsession() {
- session=$(cat "$1" | grep "^[[:space:]]*session[[:space:]]*=" | sed "s/^[[:space:]]*session[[:space:]]*=[[:space:]]*//" )
- #session=${session/#~/`getent passwd ${user}|cut -d: -f6`}
- echo $session
- }
- case "$1" in
- start)
- echo -n "Starting $DESC: $NAME"
- d_start
- echo "."
- ;;
- stop)
- echo -n "Stopping $DESC: $NAME"
- d_stop
- echo "."
- ;;
- restart|force-reload)
- echo -n "Restarting $DESC: $NAME"
- d_stop
- sleep 1
- d_start
- echo "."
- ;;
- *)
- echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
- exit 1
- ;;
- esac
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement