Scripts and Cron Jobs

These are some of the the scripts and cron jobs I use to help with things. Some of these scripts or cron jobs will be specific to the HamVoIP installation since that is what I prefer to use. When adding cron jobs to the crontab it can be a little confusing about how the time and date are setup. There is an excellent website that has a handy calculator for this here, Crontab Calculator.  
Now, if you are unfamiliar with the creation or execution of scripts here is a very short version of how to get it done. 
If you want to use a script I have here, you need to create a blank file that has the name you want for your script with .sh on the end. This is typically done use the program nano and the commands I give here will assume you are using nano as well. The CLI command would be this:

nano your_file_name.sh

Once you open this up, you can copy and paste in the text for the script. Then save the file by pressing and holding the "Ctrl" key and then pressing the "X" key and nano will ask you if you want to save the file. 
Now you have saved the file but it is not yet executable. This is done with this simple command:

chmod u+x your_file_name.sh

Now the script is executable and you can execute it manually by using this command:

./your_file_name.sh

If everything is correct your script should run and it should (hopefully) do what you want it to do. 

So, here are some helpful scripts you can use.
Pinger Script
I use this script to keep tabs on the RTCM's and voter boards to make sure they are still alive. 
It's pretty simple and I am sure there are many out there that have better solutions but this one works for me. This script requires you have SMTP setup on your node so it can send out the email when a board fails to ping. This script would be executed by a cron job, say every 15 minutes or so but it can be whatever you want it to be. 

#!/bin/bash
ping_targets="192.168.1.1"
failed_hosts=""

for i in $ping_targets
do
   ping -c 1 $i > /dev/null
   if [ $? -ne 0 ]; then
      if [ "$failed_hosts" == "" ]; then
         failed_hosts="$i"
      else
         failed_hosts="$failed_hosts, $i"
      fi
   fi
done

if [ "$failed_hosts" != "" ]; then
   echo $failed_hosts The Voter board is not reachable | mailx -s "Failed to Ping Voter Board" your_email@gmail.com

The cron job I use to execute this script every 15 minutes is this:
*/15 * * * * cd /; ./pinger.sh
Asterisk Watchdog reboot scripts
This script was created due to a very occasional issue that arises every 6 months or so. For some reason on the Pi running HamVoIP the Asterisk service will stop running and cannot be restarted without doing a complete reboot of the Raspberry Pi itself. Once that is done it chugs along for another 6 or so months. 
This reboot can be performed remotely so it's not a big issue but I wanted something that can take care of this automatically without my intervention. This script is relatively simple, it looks to see if the service is running using grep. If it doesn't find it running it issues a reboot command to Linux. The script on my system is executed every 10 minutes.

#!/bin/bash
ps -ef | grep safe_asterisk |grep -v grep > /dev/null
if [ $? != 0 ]
then
       shutdown -r now > /dev/null 
fi

This is the cron job I use to execute the script:
*/10 * * * * cd /; ./asterisk_watchdog.sh

Alternate Asterisk System Reboot Script
This does the same thing as the above script except in a different way. This is a modification of the Asterisk restart script which happens to be /usr/local/sbin/astres.sh

#! /bin/bash
if [ -e /var/run/asterisk.ctl ]
then
	echo "Asterisk is already running"        
else
	echo "Asterisk is not running! ...Rebooting the System..."
	shutdown -r now
fi

Asterisk Service check and start script
This is another modification of the astres.sh script. This simply checks if the Asterisk service is running and if it's not, starts it up. 

#! /bin/bash
if [ -e /var/run/asterisk.ctl ]
then
	echo "Asterisk is already running"     
else
	echo "Asterisk is not running! ...Starting asterisk..."
	/usr/sbin/safe_asterisk
fi

Asterisk Service Restart Script (astres.sh)
This script already exists in the installation but I thought I would list it here anyway. This script will restart Asterisk if it's already running and start it up if it's not running. 

 #! /bin/bash
if [ -e /var/run/asterisk.ctl ]
then
        echo "Restarting Asterisk..."
        asterisk -rx
else
        echo "Asterisk is not running! ...Starting asterisk..."
        /usr/sbin/safe_asterisk
fi
Cron Job to Restart Asterisk
I instituted this Cron Job due to a particular issue with Asterisk while using the Voter module. 
I found that with both the AllstarLink Official version and the HamVoIP version of Allstar that after about 3 days the Voter module within asterisk would stop working. Restarting Asterisk would solve this issue but after about 3 days it would happen again. Your mileage may vary and this may not be an issue for you but it is for me. This Cron Job restarts Asterisk every morning at 2 AM and so far this has given me trouble free, long term operation. 

0 02 * * * /usr/local/sbin/astres.sh