Skip to content

Instantly share code, notes, and snippets.

@edharman
Created November 10, 2022 08:33
Show Gist options
  • Save edharman/f9aeab9d87765a6fabc5e4918bd955fb to your computer and use it in GitHub Desktop.
Save edharman/f9aeab9d87765a6fabc5e4918bd955fb to your computer and use it in GitHub Desktop.
#!/bin/bash
# read the PID's of all RMS processes into an array and then read the number of running stations into
# array RunList so that after killing the instances we can then update RMS and then restart the stations.
# Default behaviour if called with no arguments, - capture all the running RMS processes, kill them, update RMS, then start
# all that are configured within directory ~/source/Stations -
# we need the following in either case
PidList=( $(ps -ef|awk '/[S]tartC/ {print $2}') ) # create an array of the running RMS processes
# This script when run as a crontab entry runs under root's account so we need to set some variables to allow the
# script to launch apps onto the users desktop -which it doesn't own. We also use this to build the full paths to any
# files since jobs running under cron don't inherit normal users ENV variables
# Note: this may well break if the distro used doesn't use xdm or if the user e.g. connects via xrdp....
UserDisp=($(w -h | awk '/xdm/ {print $1,$2}')) # grab the RMS username and current display number
export XAUTHORITY=/home/${UserDisp[0]}/.Xauthority
export DISPLAY=${UserDisp[1]}
# script proper...
if [[ -z "$1" ]] # called with no args
then
echo " ....Will restart all configured stations post-update"
for Dir in /home/${UserDisp[0]}/source/Stations/*
do
if [[ "${Dir##*/}" != "Scripts" ]]
then
RunList+=(${Dir##*/}) # build an array of the configured stations
fi
done
# the next line is a little tricksy.... when the 1st instance of lxterminal is launched, subsequent launches are in fact daughters of the 1st
# launched instance. So if for example the user opens a separate lxterminal to say perform some action but not running an RMS process, then this
# terminal instance is in fact owned by the 1st launched lxterminal instance which might be running RMS. So we don't want to explicitly kill this root
# terminal process since it would terminate all the users terminal sessions.
# So the 1st element in array PidList is the root lxterminal instance, because process PID's are allocated in numerically ascending order
# and ps output by default is ordered by ascending PID's, so when iterating through this array we start at index 1 and not index 0 the
# 1st array element....
for Pid in "${PidList[@]:1}"
do
kill $Pid
done
/home/${UserDisp[0]}/source/RMS/Scripts/RMS_Update.sh >/dev/null
sleep 10
for Station in "${RunList[@]}" # restart all that are configured
do
sleep 15
lxterminal --title=${Station} -e "/home/${UserDisp[0]}/source/Stations/Scripts/StartCapture.sh ${Station}" &
done
else
# If called with any argument then all running RMS processes are killed and RMS updated
# however only those only those stations that were actually running when called are restarted
# and not all the stations that are configured.
# Handy if say out of 3 stations, one is not running due to a camera issue
# grab the running station names, slightly tricky since there is only one parent lxterminal but potentially many children
RunList=( $(ps -ef|awk 'BEGIN {FS="/"} /[S]tations/ {print $(NF-1)}' |sort -u) )
for Pid in "${PidList[@]:1}"
do
kill $Pid
done
/home/${UserDisp[0]}/source/RMS/Scripts/RMS_Update.sh >/dev/null
sleep 10
for Station in "${RunList[@]}"
do
echo "starting $Station"
sleep 15
lxterminal --title=${Station} -e "/home/${UserDisp[0]}/source/Stations/Scripts/StartCapture.sh ${Station}" &
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment