Skip to content

Instantly share code, notes, and snippets.

@DaveThw
Last active November 22, 2022 15:02
Show Gist options
  • Save DaveThw/cf4aa638a03fdedfcd05 to your computer and use it in GitHub Desktop.
Save DaveThw/cf4aa638a03fdedfcd05 to your computer and use it in GitHub Desktop.
init.d script for get_iplayer web server and pvr (on a Raspberry Pi)
#!/bin/bash
### BEGIN INIT INFO
# Provides: get_iplayer_web
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start or stop the get_iplayer web server and pvr
### END INIT INFO
# Can be downloaded and installed in one go by using this command
# sudo wget -O /tmp/download https://gist.github.com/DaveThw/cf4aa638a03fdedfcd05/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/init.d && sudo chmod +x /etc/init.d/get_iplayer_web && sudo update-rc.d get_iplayer_web defaults
# Or download and just move to /etc/init.d using this command
# sudo wget -O /tmp/download https://gist.github.com/DaveThw/cf4aa638a03fdedfcd05/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/init.d && sudo chmod +x /etc/init.d/get_iplayer_web
# and then start/stop/restart the service with these commands
# sudo service get_iplayer_web stop
# sudo service get_iplayer_web start
# sudo service get_iplayer_web restart
# And then, if/when you want the service to autostart on bootup, use this
# sudo update-rc.d get_iplayer_web defaults
# Conversely, if you want to stop the service autostarting on bootup, use this
# sudo update-rc.d -f get_iplayer_web remove
# This script is based on my version of the Node-RED init.d script, here: https://gist.github.com/DaveThw/da394c8d04fa2bba55ac
# User that launches get_iplayer_web (it's advised to create a new user)
# You can do:
# sudo useradd iplayer-web
# then change line below from USER=pi to USER=iplayer-web
USER=pi
# The interface address to listen on for webpage requests:
# Use LISTEN=0.0.0.0 to listen on any/all interfaces (not very secure - use on trusted local network only - don't expose to the internet!)
# Use LISTEN=127.0.0.1 to only accept requests from this computer
LISTEN=0.0.0.0
# The port to listen on for webpage requests:
PORT=1935
# DON'T CHANGE anything below this line unless you know what you're doing!
NAME=get_iplayer_web
DAEMON="/usr/bin/perl /usr/share/get_iplayer/get_iplayer.cgi"
OPTIONS="-p $PORT -g /usr/bin/get_iplayer -l $LISTEN"
LOG="/var/log/${NAME}.log"
PIDFILE="/var/run/${NAME}.pid"
. /lib/lsb/init-functions
start_daemon () {
# If the log file doesn't yet exist, attempt to create it
! [ -e "$LOG" ] && sudo touch "$LOG"
# Check if USER is the owner of the log file, and chown if not
[ -e "$LOG" ] && [ -z "$(find "$LOG" -user $USER)" ] && sudo chown $USER "$LOG"
echo >> $LOG
date >> $LOG
echo "Starting daemon $NAME with command '$DAEMON' and options '$OPTIONS'..." >> $LOG
echo >> $LOG
start-stop-daemon --start --background \
--chuid $USER \
--name $NAME \
--make-pidfile --pidfile $PIDFILE \
--startas /bin/bash -- -c "exec $DAEMON $OPTIONS >> $LOG 2>&1"
# check if the daemon actually started, and return an appropriate result with log_end_msg
status="0"
pidofproc $DAEMON >/dev/null || status="$?"
log_end_msg $status
}
stop_daemon () {
echo >> $LOG
date >> $LOG
echo "Stopping daemon $NAME..." >> $LOG
echo >> $LOG
status="0"
start-stop-daemon --stop --signal INT --quiet \
--chuid $USER \
--exec $DAEMON --pidfile $PIDFILE --retry 30 \
--oknodo || status="$?"
log_end_msg $status
}
case "$1" in
start)
log_daemon_msg "Starting daemon" "$NAME"
start_daemon
;;
stop)
log_daemon_msg "Stopping daemon" "$NAME"
stop_daemon
;;
restart)
log_daemon_msg "Stopping daemon" "$NAME"
stop_daemon
sleep 5
log_daemon_msg "Re-Starting daemon" "$NAME"
start_daemon
;;
status)
status_of_proc "$DAEMON" "$NAME"
exit $?
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac
exit 0
@confusedpublic
Copy link

Hi,

I've just used this script to run the web pvr a daemon on OSMC. I'm running the daemon as user osmc, and only changed the name of the daemon. It seems to run fine in that I can access the web page and the pvr manager adds programmes. However, when checking the status of the daemon after launch, I am presented with the following lines:

Sep 09 12:46:25 osmc get_iplayer_web_pvr[17193]: Starting daemon: get_iplayer_web_pvrstart-stop-daemon: warning: this system is not able to track process names
Sep 09 12:46:25 osmc get_iplayer_web_pvr[17193]: longer than 15 characters, please use --exec instead of --name.
Sep 09 12:46:25 osmc get_iplayer_web_pvr[17193]: /etc/init.d/get_iplayer_web_pvr: invalid arguments
Sep 09 12:46:25 osmc get_iplayer_web_pvr[17193]: failed!

I'm struggling to see which arguments are apparently invalid, and what has failed. Any clue/help?

Thanks for the script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment