Channel changing script runs twice

For discussion of topics specific to MythTV on linux
Post Reply
rwiskow
Newcomer
Posts: 4
Joined: Mon Nov 21, 2022 4:24 pm
United States of America

Channel changing script runs twice

Post by rwiskow »

After migrating my Myth backend to some new hardware, I switched to using a channel changing script that sends commands via UPNP to my STB. This works fairly well, except that if there's both an SD and an HD version of the channel, the UPNP call to set channel will set it to the SD.
I modified the script so that it can follow the set channel with an increment channel command to bump it up to the HD channel. By itself this seems to work pretty well.
However when a scheduled recording would start up, it would sometimes end up on the wrong channel. After adding some logging output from the channel changing script, I discovered that it appears that Myth is sometimes running the script twice (concurrently). When this happens, it ends up setting the channel twice, but then doing two more increment channels.
I tried to use some sort of locking mechanism (flock, and some others) to only allow one instance of the script to run, but that wasn't working at all when Myth ran it. Myth seems to be running the script in some odd shell state.

Any ideas on how to get Myth to stop running the script twice? (Seems like a bug). Or any ideas on how to prevent the increment channel from being called twice?
Dai_Trying
Junior
Posts: 51
Joined: Tue May 24, 2022 10:18 am
Great Britain

Re: Channel changing script runs twice

Post by Dai_Trying »

You could try checking for the script already running and not run it if it has already started, the below snippet works in bash.

Code: Select all

Running=$(ps -ae | grep -c test.sh)

if [[ $Running > 2 ]]; then
  echo "Already running"
  exit 99
fi
Just change "test.sh" to the name of your script and place it at the beginning of your own script and it should work.

EDIT: You can also remove the echo statement, it was just there for testing.
rwiskow
Newcomer
Posts: 4
Joined: Mon Nov 21, 2022 4:24 pm
United States of America

Re: Channel changing script runs twice

Post by rwiskow »

Thanks, that seems to have worked... though I'm not sure if it's because it's actually exiting the extra running script, or if it's mysteriously preventing the second script from running in the first place. To verify that it was working properly, I redirected the echo statement out to my log file, but it never seems to log that. It's just got the single run that actually changes the channel.
I guess I'll let it keep running and see what happens with the regular schedule.
jfbauer
Newcomer
Posts: 13
Joined: Tue Jul 26, 2022 11:43 pm
United States of America

Re: Channel changing script runs twice

Post by jfbauer »

In the first reply that "> 2" might need to be "> 1". You might also need to exit 0 if script is already running. Mythtv might consider anything else an error and just try again.

Also check out the BashFAQ section on this for some ideas.

Post your script and we'll take a look at it.
Dai_Trying
Junior
Posts: 51
Joined: Tue May 24, 2022 10:18 am
Great Britain

Re: Channel changing script runs twice

Post by Dai_Trying »

jfbauer wrote:
Fri Feb 17, 2023 3:02 pm
In the first reply that "> 2" might need to be "> 1".
This is set to 2 because grep finds it own command call which contains the script name and so it has to be 2 for it to work correctly, otherwise it will always say it is running.
User avatar
jhoyt
Senior
Posts: 143
Joined: Thu Aug 27, 2015 10:11 am
United States of America

Re: Channel changing script runs twice

Post by jhoyt »

I saw / see the same thing and opened an issue in github. Please add any details.

https://github.com/MythTV/mythtv/issues/584

The solution I went with was to put a lock file in place (code below)

Code: Select all

CHANNEL_LOCK=/tmp/channelchange_lirc.lock

# Check is Lock File exists, if not create it and set trap on exit
if { set -C; 2>/dev/null >$CHANNEL_LOCK; }; then
  trap "rm -f $CHANNEL_LOCK" EXIT
else
  # log failed attempt
  logger "ir_channel_change lirc channel change lock file exists… exiting"
  sleep 10
  exit
fi

# log untrapped channel change event
CHANNEL=${1}
logger "ir_channel_change: lirc attempting to change channels to" ${1}
rwiskow
Newcomer
Posts: 4
Joined: Mon Nov 21, 2022 4:24 pm
United States of America

Re: Channel changing script runs twice

Post by rwiskow »

I was running into odd issues when I tried to use a lockfile, though I was attempting to use flock and lockfile calls. The ps | grep method seems to be working for me for now.
Post Reply