t
o
n
y
a
n
g
'
s
 
w
e
b
l
o
g


today i tried my hands at writing a script that would automatically log back into the omxplayer if it lost the rtsp stream connection. omxrtspping.sh:

#!/bin/bash

if [[ $(tty) == /dev/tty[0-9]* ]] ; then
   echo rtspPing.sh : ping omxplayer every 60 seconds : restart omxplayer

   script=omxrtspstart.sh

   PID=$(pidof omxplayer.bin)
   echo PID = $PID

   while [ $PID ]
   do
      sleep 60
      PID=$(pidof omxplayer.bin)
   done

   sleep 15
   exec $script
fi

i used a variable tty comparison that can screen for all numbered iterations of /dev/tty. the original script that i found online used this to get the PID of the omxplayer.bin: PID=$(ps ax | grep omxplayer.bi[n] | cut -d' ' -f2). but when i tested it out, i noticed that it only worked when the PID was 4-digits; anything less and it'd return a blank space. but then i discovered the pidof command, which does the same thing and doesn't have the delimiting bug. i also discovered that bash scripts are very picky when it comes to spaces with a line of code. the reason why some of my previous script attempts didn't work was because of misplaced spaces.

omxrtspstart.sh:

#!/bin/bash

if [[ $(tty) == /dev/tty[0-9]* ]] ; then
   echo rtspStart.sh : starting omxplayer rtsp stream
   omxplayer -b -o hdmi rtsp://admin:password@12.34.567.89:80/videoSub
fi

and then at the bottom of my .bashrc i added these two lines:

bash omxrtspstart.sh &
bash omxrtspping.sh

in the afternoon i went to market basket to get some eggs, some bean sprouts, a box of clementines, and a bottle of drain-o. while getting out my bicycle, a little lady swearing in italian pushed a cart towards me, trapping me inside a wall of shopping carts. i yelled at her to come back, but she ignored me. later i saw her again, smiling, walking into the supermarket. from there i went up webster avenue to the cambridge street family dollar store, to get some plastic bins ($3/each) for my mother. i returned home via inman square.

and making a quick stop at the house, i left again, this time to the cafe to drop off the bean sprouts and the plastic bins. the cafe reeked of boiled cabbage. i had some sweet silver ear mushroom soup. i tested out the pi zero which earlier i'd remotely changed the scripts. when the pi rebooted, it just went into the desktop, didn't play the rtsp stream at all. whatever i did ended up breaking it, so i reverted everything back to the way it was before, using juiceSSH on my phone.

back at home, i worked to debug the omxplayer rebooting script, using the other pi zero at home to test out the code. i played around with the scripts but i just couldn't get it to work. the worst was when i got the script respawning the omxplayer with new PID's. i couldn't kill the processes fast enough so i ended up removing the code from .bashrc and rebooting. the script that checks to see if the omxplayer was active also has a habit of disappearing after it makes a script call.

i decided to try making the scheduled calls with a cron job, which is what it's designed to do. i create a cron script rtspCron.sh:

#!/bin/bash

PID=$(pidof omxplayer.bin)

if [ ! $PID ] ; then
   echo rtspStart.sh : starting omxplayer rtsp stream
   omxplayer -b -o hdmi rtsp://admin:password@12.34.567.89:80/videoSub
fi

later in the evening i realized i could've done all that in just a single line of code: pidof omxplayer.bin || omxplayer -b -o hdmi rtsp://admin:password@12.34.567.89:80/videoSub (maybe i'll change it afterwards). and to the crontab -e i added these two lines:

@reboot bash /home/pi/rtspStart.sh
*/1 * * * * /home/pi/rtspCron.sh

after a reboot, the rtsp video stream came on. via SSH i went to inspect the processes. i couldn't see where omxplayer was being called, until i did a more thorough ps auwx and saw it was running as root (not as pi, which meant it was called during boot; also TTY was unknown (?)). i then killed the omxplayer process (dumped into the GUI desktop) and waited for the cron job to cycle 60 seconds and call the rtspCron.sh script. sure enough, after a bit of time, the rtsp video stream started playing again. success! although i got it to work, i'd still like to know why it didn't work when i had .bashrc calling the scripts.

freddy came home relatively early, around 6:20pm. he made some soup and ate quietly in the darken half of the kitchen by the rarely-used dining table. i opened a light for him. he seemed sad, and i thought i heard him snifling, like maybe he was crying. the typical routine is he comes home, fixes dinner in the kitchen, then disappear into his bedroom. we don't have much to talk about, his english is okay at best, and he rarely initiates any conversations. he's just a quite person, maybe a bit shy, which must be difficult since he really stands out with his height. he's got 9 more days left in boston anyway before he leaves.

as for me, i ate two slices of leftover ricotta pie for dinner.