PlexConnect init script for Fedora 19

The original code is authored by wilsoncd2 at plexapp.com.

https://forums.plexapp.com/index.php/topic/71220-linux-install-for-plex-connect/?hl=linux

I modified the script to utilize init.d  functions rather than lsb.

#!/bin/bash
### BEGIN INIT INFO
# Provides:          plexconnect
# Required-Start:    plexmediaserver networking
# Required-Stop:     plexmediaserver networking
# Default-Start:     3 4 5
# Default-Stop:      0 1 6
# Short-Description: This is the Plex Connect daemon
# Description:       This script starts the Plex Connect
#                    Python scripts in a detached screen.
### END INIT INFO

# Using functions to perform the operations.
. /etc/rc.d/init.d/functions

# Process name ( For display )
NAME=PlexConnect

# Daemon name, where is the actual executable
DAEMON="/usr/bin/screen"
DAEMON_OPTS="-S PlexConnect -d -m /usr/local/lib/plexconnect/PlexConnect.py"
DAEMON_USER="root"

# pid file for the daemon
PIDFILE=/var/run/PlexConnect.pid
# If the daemon is not there, then exit.
test -x "$DAEMON" || exit 5

case $1 in

 start)
  # Checked the PID file exists and check the actual status of process
  if [ -e $PIDFILE ]; then
   pidofproc -p $PIDFILE > /dev/null 2>&1
   # If the status is SUCCESS then don't need to start again.
   if [ $? = "0" ]; then
    log_success_msg "Starting the process $NAME"
    exit # Exit
   fi
  fi
  # Start the daemon.
  # Start the daemon with the help of start-stop-daemon
  # Log the message appropriately
  if start_daemon -u $DAEMON_USER -p $PIDFILE $DAEMON $DAEMON_OPTS; then
   while read line ; do [[ $line =~ ([0-9]*).PlexConnect ]] && echo ${BASH_REMATCH[1]} ; done < <(screen -ls) > $PIDFILE
   log_success_msg "Starting the process $NAME"
  else
   log_failure_msg "Starting the process $NAME"
  fi
  ;;
 stop)
  # Stop the daemon.
  if [ -e $PIDFILE ]; then
   pidofproc -p $PIDFILE > /dev/null 2>&1
   if [ "$?" = 0 ]; then
    killproc -p $PIDFILE
    /bin/rm -rf $PIDFILE
    log_success_msg "Stopping the $NAME process"
   fi

  else
   log_failure_msg "$NAME process is not running"
  fi
  ;;
 restart)
  # Restart the daemon.
  $0 stop && sleep 2 && $0 start
  ;;
 status)
  # Check the status of the process.
  if [ -e $PIDFILE ]; then
   pidofproc -p $PIDFILE > /dev/null 2>&1
   if [ "$?" = 0 ]; then
    log_failure_msg "$NAME PID file exists, but the process dead"
    killproc -p $PIDFILE
    /bin/rm -rf $PIDFILE
   else
    log_success_msg "$NAME process is running"
   fi
  else
   log_failure_msg "$NAME process is not running" #log_end_msg 0
  fi
  ;;
 reload)
  $0 restart
  ;;
 *)
  # For invalid arguments, print the usage message. echo "Usage: $0 {start|stop|restart|reload|status}"
  exit 2
 ;;
esac

David Gregory Medina

Post created by: David Gregory Medina

Tagged with: , , , , , ,

Leave a Reply