Created
July 13, 2018 03:39
-
-
Save Sparrow1029/0003ae2c34502b52b1c9fedefdf83eaa to your computer and use it in GitHub Desktop.
Plex Media Server helper script - bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script makes it easier to activate and restart plexmediaserver from | |
# the command line, as well as enabling/disabling the server at boot. | |
read -d '' USAGE << EOF | |
usage: plex start|stop|disable|enable|status | |
help - print this text and exit. | |
start|stop - start and stop plexmediaserver service (active|inactive) | |
enable|disable - toggle plexmediaserver at system start (enabled|disabled) | |
status - show current status of plex (ex: "active::disabled") | |
EOF | |
if [[ $# -ne 1 ]]; then | |
echo "$USAGE" | |
exit 0 | |
fi | |
# Was using this below, but opted for each individual command to use sudo. | |
#if [[ $EUID -ne 0 ]]; then | |
# echo "This script must be run with sudo "$0" instead." 1>&2 | |
# exit 1 | |
#fi | |
SERVICE=plexmediaserver.service | |
RUNNING=$(systemctl is-active $SERVICE) | |
ENABLED=$(systemctl is-enabled $SERVICE) | |
# status colors | |
normal="\033[0m" | |
lt_red="\033[91m" | |
lt_green="\033[92m" | |
bold="\033[1m" | |
color_script () { | |
case $1 in | |
'inactive') | |
echo -e -n "\t${bold}${lt_red}$1${normal}" | |
;; | |
'active') | |
echo -e -n "\t${bold}${lt_green}$1${normal}" | |
;; | |
'enabled') | |
echo -e "${bold}${lt_green}$1${normal}" | |
;; | |
'disabled') | |
echo -e "${bold}${lt_red}$1${normal}" | |
;; | |
esac | |
} | |
restart_plex() { | |
local ANS | |
sleep 1 | |
if [ "$RUNNING" == "inactive" ]; then | |
echo "plex not currently active." | |
echo -n "Would you like to start it now? [Y/n] " | |
while :; do | |
read ANS | |
[[ $ANS =~ ^(y|Y|^$)$ ]] && break | |
[[ $ANS =~ ^(n|N)$ ]] && exit 0 | |
echo -n "Invalid input, please try again [Y/n] " | |
done | |
sudo service plexmediaserver start | |
echo "Plex started!" | |
exit $? | |
elif [[ "$RUNNING" == "active" ]]; then | |
echo "Restarting ..." | |
sudo systemctl try-restart $SERVICE | |
echo -e "${lt_green}Done!${normal}" | |
exit $? | |
fi | |
} | |
case "$1" in | |
'start') | |
if [ "$RUNNING" == "inactive" ]; then | |
sudo service plexmediaserver start | |
echo "Plex started!" | |
exit $? | |
else | |
echo "Plex is already running!" | |
echo "(plex stop to turn it off)" | |
exit $? | |
fi | |
;; | |
'stop') | |
if [ "$RUNNING" == "active" ]; then | |
sudo service plexmediaserver stop | |
echo "Plex is now inactive." | |
else | |
echo "Plex isn't currently running." | |
echo "(plex start to turn on)" | |
exit $? | |
fi | |
;; | |
'restart') | |
restart_plex | |
exit $? | |
;; | |
'enable') | |
if [ "$ENABLED" == "disabled" ]; then | |
sudo systemctl enable plexmediaserver | |
echo "Enabled at system boot. Restart required for changes to take effect." | |
else | |
echo "Plex already enabled at system boot." | |
exit $? | |
fi | |
;; | |
'disable') | |
if [ "$ENABLED" == "enabled" ]; then | |
sudo systemctl disable plexmediaserver | |
exit $? | |
fi | |
;; | |
'status') | |
echo "" | |
color_script $RUNNING && echo -n "::" && color_script $ENABLED | |
echo "" | |
#echo -e "$RUNNING; $ENABLED" | |
exit $? | |
;; | |
*) | |
echo "invalid argument: $1" | |
exit $? | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment