How to detect url in use in a script?

Hello,
I have a small script and it runs from web application in below format:

pipe:///path_to_myscript.sh url1 url2 url3

myscript.sh:

#!/bin/bash
count=0
while [ "$count" -lt "20" ]
do
count=$((count+1))
exec 3>&1
((ffmpeg -i $1 ...... -f mpegts pipe:1 2>/dev/null 1>&3 ) 2>&1 | \
while read LINE; do echo "$1 $LINE"; done 1>&2 ) 3>&1
PID=$!
wait ($PID)
if [ -n "$2" ]; then
((ffmpeg -i $2 ...... -f mpegts pipe:1 2>/dev/null 1>&3 ) 2>&1 | \
while read LINE; do echo "$2 $LINE"; done 1>&2 ) 3>&1
PID=$!
wait ($PID)
fi
if [ -n "$3" ]; then
((ffmpeg -i $3 ..... -f mpegts pipe:1 2>/dev/null 1>&3 ) 2>&1 | \
while read LINE; do echo "$3 $LINE"; done 1>&2 ) 3>&1
PID=$!
wait ($PID)
fi
sleep 1
done

When I run it, url1, or 2 or 3 runs but I am unable to see which one is in use and I do not wish to run iftop to detect. Htop is not giving any clue.
How may I change this script to be able to see the active url?

Thanks in advance
Boris

I might be a little slow but i can't understand what exactly you want - and, to be honest, the analysis of your script doesn't make it any clearer for me. Could you please explain (for dummies like me) what exactly you want to achieve?

Thank you.
bakunin

Dear Bakunin,
As the question was not directly related to ffmpeg, I took corresponding portion off the code.
Let's say I have three different tv channel url from three different domain addresses.
The script runs each of them in given order (url 1->2>3). When url1 is down for some technical issues, script goes to url2 . When url2 goes down, it goes to url3 .
When url3 is down, I can make the script respawn so it goes back to url1 once again.
What I'd like to do is to see which one is responding to my script in command line environment.

If I type:

ps aux | grep myscript.sh

it just gives:

myscript.sh url1 url2 url3 

but what I need to see is just:

myscript.sh url$

Thanks
Boris

If there is gui. insert before each line ((ffmpeg...

notify-send "Your message, eg start urlX"

in linux command notify-send there is in package libnotify (rhel)
or libnotify-bin (debian)

1 Like

I have to second bakunin in that your intentions with the script are way less than clear.

I didn't find (maybe overlooked?) the necessary conditional execution in your script. It will run the n (up to three) compound commands, one after the other, regardless of the respective exit codes, and loop until count equals 20. So, 60 commands...
On top, I'm having a hard time to understand your juggling with file descriptors 2 and 3 - what do you want to achieve here?

And, the PID variable used for the (useless) trap won't be set anywhere as ( man bash )

and you're not running anything in background (which would be pointless anyhow as you want to run additional processes only on failure of the predecessors)

Last but not least, is that on purpose that you echo "$1 $LINE" in all three branches?

Dear Rudic,
Thank you for your post.
Regarding below comment, how may I make it terminating current subprocess when related url is off?

Dear Nezabudka,
Thank you for notify tool, I have just installed it and implemented into my script but it shows just the first one.
Original format of url1: http://xx.yy.zz/aa/index.m3u8
I made it: http://x__x.yy.zz/aa/index.m3u8
In that case, it insisted on showing the wrong format as if it was responding but in the background it plays the second one or third one I think.

Thank You All
Boris

Wouldn't that command just fail and exit (with respective exit code)?

1 Like

Dear Rudic,
I added wait $PID before each fi in the script.
Now I can see the active url on terminal with

ps aux | grep myscript.sh

Thank you for your time
Boris

Not with the script given in post#1. No background process, no wait ($PID) .

Thanks..
Have just updated.
Removed trap , wait $pid* added.

Log:

2018-12-10 10:48:00.601 spawn: Executing "/root/tmp/myscript.sh"
2018-12-10 10:48:00.897 spawn: $url1 ffmpeg version N-92392-g16d91b7 Copyright (c) 2000-2018 the FFmpeg developers
2018-12-10 10:48:00.897 spawn: $url1 built with gcc 7 (Ubuntu 7.3.0-27ubuntu1~18.04)
2018-12-10 10:48:00.897 spawn: $url1 configuration: --prefix=/root/ffmpeg_build --pkg-config-flags=--static --extra-cflags=-I/root/ffmpeg_build/include --extra-ldflags=-L/root/ffmpeg_build/lib --extra-libs='-lpthread -lm' --bindir=/root/bin --enable-gpl --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-nonfree --enable-openssl
Program 1
2018-12-10 10:48:02.006 spawn: $url1 Metadata:
2018-12-10 10:48:02.006 spawn: $url1 service_name    : Service01
2018-12-10 10:48:02.006 spawn: $url1 service_provider: FFmpeg
2018-12-10 10:48:02.006 spawn: $url1 Stream #0:0[0x100]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], 25 fps, 25 tbr, 90k tbn, 50 tbc
2018-12-10 10:48:02.006 spawn: $url1 Stream #0:1[0x101]: Audio: aac (LC) ([15][0][0][0] / 0x000F), 48000 Hz, stereo, fltp, 133 kb/s
2018-12-10 10:48:02.006 spawn: $url1 Overriding aspect ratio with stream copy may produce invalid files
2018-12-10 10:48:02.006 spawn: $url1 Stream mapping:
2018-12-10 10:48:02.006 spawn: $url1 Stream #0:0 -> #0:0 (copy)
2018-12-10 10:48:02.006 spawn: $url1 Stream #0:1 -> #0:1 (aac (native) -> mp2 (native))
2018-12-10 10:48:02.006 spawn: $url1 Press [q] to stop, [?] for help
2018-12-10 10:48:02.007 spawn: $url1 Output #0, mpegts, to 'pipe:':
2018-12-10 10:48:02.007 spawn: $url1 Metadata:
2018-12-10 10:48:02.007 spawn: $url1 encoder         : Lavf58.22.100
2018-12-10 10:48:02.007 spawn: $url1 Stream #0:0: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 25 fps, 25 tbr, 90k tbn, 25 tbc
2018-12-10 10:48:02.007 spawn: $url1 Stream #0:1: Audio: mp2, 48000 Hz, stereo, s16, 160 kb/s
2018-12-10 10:48:02.007 spawn: $url1 Metadata:
2018-12-10 10:48:02.007 spawn: $url1 encoder         : Lavc58.39.100 mp2

Kind regards
Boris