Build a tool to keep the stream up in case it goes down (Part 3)

This is a 4 part series. The parts are as follows:

  1. Raspberry Pi with scanner to local casting server.
  2. Raspberry Pi with scanner to BroadCastify Stream.
  3. Build a tool to keep the stream up in case it goes down.
  4. Raspberry Pi with rtl_sdr dongle to BroadCastify Stream.

We need to configure this so it is more of an auto-magic thing versus so much manual work.

To start, I want to have this start up when the Raspberry Pi boots. I created the following file in /etc/init.d/ and called it bcfy_start.sh

Then I ran

$ sudo chmod +x /etc/init.d/bcfy_start.sh

to make the file executable. A boot file will need a few key pieces of info in it to ensure the Operating System knows how it should be handled and when. The init script needs to be LSB (Linux Standard Base) compliant.

#!/bin/sh

### BEGIN INIT INFO

# Provides:          bcfy_start.sh

# Required-Start:    $remote_fs $syslog $network

# Required-Stop:     $remote_fs $syslog

# Default-Start:     2 3 4 5

# Default-Stop:      0 1 6

# Short-Description: Start daemon at boot time

# Description:       Enable service provided by daemon for BroadCastify.

### END INIT INFO

arecord -f dat -c 1 -D hw:1,0 | /usr/bin/lame -r -s 48 --resample 44.1 -m m -b 16 -F --cbr --lowpass 2.8 --highpass .4 - - | /usr/bin/ezstream -c /etc/ezstream_bcfy.xml &

Required-Start: $remote_fs $syslog $network

  • defines facilities that must be available to start the script. Consider using virtual facility names as described below if adequate. If no boot facility is specified it means that this script can be started just after the bootstrap without local filesystems mounted, nor system logger, etc.

$remote_fs

all filesystems are mounted. In some LSB run-time environments, filesystems such as /usr may be remote. If the script need a mounted /usr/, it needs to depend on $remote_fs. Scripts depending on $remote_fs do not need to depend on $local_fs. During shutdown, scripts that need to run before sendsigs kills all processes should depend on $remote_fs.

$network

low level networking (ethernet card; may imply PCMCIA running)

Required-Stop: $remote_fs $syslog 

  • defines facilities used by the service provided by the script. The facility provided by this script should stop before the listed facilities are stopped to avoid conflicts. Normally you would include here the same facilities as for the Required-Start keyword.

$syslog

system logger is operational

Default-Start: run_level_1 [run_level_2…]

Default-Stop: run_level_1 [run_level_2…]

  • defines the run levels where the script should be started (stopped) by default. For example, if a service should run in runlevels 3, 4, and 5 only, specify “Default-Start: 3 4 5” and “Default-Stop: 0 1 2 6”.

Systems conforming to the Linux Standard Base (LSB) need not provide the exact run levels given here or give them the meanings described here, and may map any level described here to a different level which provides the equivalent functionality.[1]

LSB 4.1.0
ID Name Description
0 Halt Shuts down the system.
1 Single-user mode Mode for administrative tasks.[2][b]
2 Multi-user mode Does not configure network interfaces and does not export networks services.[c]
3 Multi-user mode with networking Starts the system normally.[1]
4 Not used/user-definable For special purposes.
5 Start the system normally with appropriate display manager (with GUI) Same as runlevel 3 + display manager.
6 Reboot Reboots the system.

So I have found that the stream also gets overruns several hours in. I am not sure why this is happening but to make sure the stream stays up until it can be fixed properly I had to build a tool.

This script checks for lame to be running every minute. If lame is not running then it shuts down the ezstream portion if it exists and starts everything again from scratch.

I created a script called /etc/scripts/bcfy_check.sh and made it executable just like above. 

 $sudo chmod +x /etc/init.d/bcfy_check.sh

Here are the contents:

#!/bin/bash

pidof  lame >/dev/null

if [[ $? -ne 0 ]] ; then

echo "Stopping old processes: $(date)" >> /var/log/bcfy.log

kill $(pidof ezstream | awk '{print $1}')

echo "Restarting BCFY script:     $(date)" >> /var/log/bcfy.log

/etc/init.d/bcfy_start.sh

fi

We made this log (into /var/log/bcfy.log) whatever it does when it changes services and tell us info we may need later. I had an issue making sure this was running at first and to make it easy on myself I also created a third script to check hourly (instead of every minute) for a PID (Process ID) for the lame application. If it finds a PID then it logs that as well as the PID of the ezstream process as well. this last script is beyond not needed but I threw it in because I used in in creating/testing all this so I put it here as well.

/etc/scripts/bcfy_check_hourly.sh

#!/bin/bash

pidof  lame >/dev/null

if [[ $? -ne 0 ]] ; then

echo "Hourly Process Logging PID of lame $(pidof lame) and ezstream $(pidof ezstream)" >> /var/log/bcfy.log

else echo "All Fine on $(date) for lame PID: $(pidof lame) and ezstream PID: $(pidof ezstream)" >> /var/log/bcfy.log

fi

Now that we have the scripts we would want them to run:

  • bcfy_check.sh – Every minute
  • bcfy_check_hourly.sh – Every hour

so we use an editor for “/etc/crontab” and the following lines are added to the end:

* * * * *       root    /etc/scripts/bcfy_check.sh
59 * * * *      root    /etc/scripts/bcfy_check_hourly.sh

*****EDIT:

I have received an email asking about eh above and realized that if you are not used to Linux then there may be an issue here as you would not know how to use an editor. Although there are some configurations that would not allow the following commands to work, you may want to try copy/pasting the following 2 commands into the command line if you do not know how to use an editor. The following 2 commands will append the syntax for the programs to the end of crontab (the linux program run scheduler)…

echo “* * * * *       root    /etc/scripts/bcfy_check.sh” >> /etc/crontab

echo “59 * * * *      root    /etc/scripts/bcfy_check_hourly.sh” >> /etc/crontab

END EDIT*****

We will pick this up next time with making the Raspberry Pi work with the rtl_sdr dongle  instead of the scanner and streaming this to Broadcastify.

One last note. I found that the console (when the overruns happen) fills the buffer and dumps a massive amount into logs. I did not want the risk of this filling logs while I was away so I simply commented (The thing we do with “#” in front of sections to designate we do not want that running) out the console portions of “/etc/rsyslog.conf”.

Click here for my club repeater linked to BroadCastify

Have fun and 73’s,

Let me know what questions you want me to address or if anything is unclear. This is a bit of technical information to understand at once so please feel free to tell me to adjust my style if it helps you understand what I am doing better.

My email is k0fey@arrl.net

radioactive

Advertisements

About K0FEY

I am a member of the Eden Prairie Radio club. This is a Minnesota Ham Radio Enthusiast club and we promote experimentation and learning through the Amateur Radio hobby. Come check into a net on Sunday with us or better yet meet us in person on the 3rd Thursday of the month.
This entry was posted in Ham Radio, Linux, Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s