From f2b6b87b568970484f3cae923a7d8d70e770cc3c Mon Sep 17 00:00:00 2001 From: Justin Emter Date: Wed, 9 Aug 2017 00:30:02 -0700 Subject: [PATCH] Added startstop.sh file to easily start / stop PseudoChannel.py in the background. Also loggin support in ./pseudo-channel.log --- .gitignore | 2 ++ PseudoChannel.py | 7 ++++++- startstop.sh | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100755 startstop.sh diff --git a/.gitignore b/.gitignore index 92eacf5..8eaab71 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ old/ pseudo-channel.db *.db-journal env/ +*.log +*.pid diff --git a/PseudoChannel.py b/PseudoChannel.py index 029709d..6376d3a 100644 --- a/PseudoChannel.py +++ b/PseudoChannel.py @@ -16,6 +16,7 @@ from plexapi.server import PlexServer import sys import datetime from datetime import time +import logging import calendar import itertools import argparse @@ -56,6 +57,8 @@ class PseudoChannel(): def __init__(self): + logging.basicConfig(filename="pseudo-channel.log", level=logging.INFO) + self.db = PseudoChannelDatabase("pseudo-channel.db") self.controller = PseudoDailyScheduleController( @@ -1060,7 +1063,7 @@ if __name__ == '__main__': """ - + logging.info("+++++ Running PseudoChannel.py -r") def trigger_what_should_be_playing_now(): @@ -1233,6 +1236,8 @@ if __name__ == '__main__': if sleep_before_triggering_play_now: + logging.info("+++++ Successfully started PseudoChannel.py") + trigger_what_should_be_playing_now() sleep_before_triggering_play_now = 0 diff --git a/startstop.sh b/startstop.sh new file mode 100755 index 0000000..2e97c34 --- /dev/null +++ b/startstop.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +# file: startstop.sh + +#---- +# Simple script to start / stop PseudoChannel.py +#---- + +#---- +# To Use: +# Just run: "./startstop.sh". If the process is running it will stop it or it will start it if not. +#---- + +#----BEGIN EDITABLE VARS---- + +pid_file=running.pid + +output_pid_path=. + +python_to_use="$(which python)" + +#----END EDITABLE VARS------- + +if [ ! -e $output_pid_path/$pid_file ]; then + + # If the running.pid file doesn't exists, create it, start PseudoChannel.py and add the PID to it. + nohup $python_to_use ./PseudoChannel.py -m -r > /dev/null 2>&1 & echo $! > $output_pid_path/$pid_file + + echo "Started PseudoChannel.py @ Process: $!" + echo "Created $pid_file file in $output_pid_path dir" + +else + + # If the running.pid exists, read it & try to kill the process if it exists, then delete it. + the_pid=$(<$output_pid_path/$pid_file) + rm $output_pid_path/$pid_file + echo "Deleted $pid_file file in $output_pid_path dir" + kill $the_pid + while [ -e /proc/$the_pid ] + do + echo "PseudoChannel.py @: $the_pid is still running" + sleep .6 + done + echo "PseudoChannel.py @: $the_pid has finished" + +fi \ No newline at end of file