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?
Channel changing script runs twice
-
- Junior
- Posts: 51
- Joined: Tue May 24, 2022 10:18 am
Re: Channel changing script runs twice
You could try checking for the script already running and not run it if it has already started, the below snippet works in bash.
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.
Code: Select all
Running=$(ps -ae | grep -c test.sh)
if [[ $Running > 2 ]]; then
echo "Already running"
exit 99
fi
EDIT: You can also remove the echo statement, it was just there for testing.
Re: Channel changing script runs twice
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.
I guess I'll let it keep running and see what happens with the regular schedule.
Re: Channel changing script runs twice
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.
Also check out the BashFAQ section on this for some ideas.
Post your script and we'll take a look at it.
-
- Junior
- Posts: 51
- Joined: Tue May 24, 2022 10:18 am
Re: Channel changing script runs twice
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)
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}
Re: Channel changing script runs twice
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.