Mar 012013
 

One of my headless wireless devices have been freezing lately, and since it’s in a somewhat isolated location, I have no idea when it freezes.
So I wrote up a quick script to check to see whether it’s up and I’m running it every hour so that it alerts me when it goes down. It will also alert me once it comes back up, in case it comes back up by itself !

The script will also save the last state that the device was in so that it will only email me once when the device goes down.


#!/bin/bash
email=YOUR@EMAIL.HERE

ip=$1
if [[ -z $1 ]]; then
echo Usage: ./pingcheck.sh \
exit
fi
if [ ! -f /tmp/$1.status ]; then
touch /tmp/$1.status
fi

oldstatus=`cat /tmp/$1.status`
reply=`ping -c 1 $ip | grep 64\ bytes | wc -l`
echo $reply > /tmp/$1.status

if [[ $oldstatus -ne $reply ]]; then
if [[ $reply -eq 1 ]]; then
echo $1 is online | mail -s "Node is online" $email
else
echo $1 is offline | mail -s "Node is offline" $email
fi
fi

Share
Feb 102013
 

As a follow up this this post, the script that I used had a few issues running on the server that I was using it on.
For some reason, on my server, the http interface would randomly die on me and then the script would not be able to stop or start motion.

So I modified the old one to this one, but it necessitated some modifications to the startup script for motion as trying to start motion via a shell script did not work due to the paths used within the startup script.

In the startup script /etc/init.d/motion, the full path is not used for start-stop-daemon. This means that when motion is started from a shell script, the command start-stop-daemon is not found. To rectify this, add /sbin/ to all instances of start-stop-daemon. The following example shows one of the instances of start-stop-daemon. The example begins on line 50, and the start-stop-daemon command is the last line of the example.

case "$1" in
start)
if check_daemon_enabled ; then
if ! [ -d /var/run/motion ]; then
mkdir /var/run/motion
fi
chown motion:motion /var/run/motion

log_daemon_msg "Starting $DESC" "$NAME"
if /sbin/start-stop-daemon --start --oknodo --exec $DAEMON -b --chuid motion ; then

In the updated script, I have added a few checks to ensure that the http interface is still running, and if it isn’t the script will kill motion altogether, and then restart motion to get everything up and running again.

The updated script is below –

#!/bin/bash
PeopleAround=0
people=( 192.168.2.72 192.168.2.117 192.168.2.129 )

wget -o getlog -O /usr/scripts/status http://localhost:8080/0/detection/status
if [ -f /usr/scripts/status ]; then
echo "SettingMotion"
MotionStarted=`cat /usr/scripts/status | sed -e 's/<[^>]*>//g' | tail -n 3 | head -n -2`
else
echo "Status file not available"
echo "Attempting to start motion"
/etc/init.d/motion start
exit
fi

sleep 5

if [ -f /usr/scripts/status ]; then
echo Checking Size
StatusSize=$(stat -c%s /usr/scripts/status)
if [[ $StatusSize -eq 0 ]]; then
echo Size is zero
MotionStarted=`pidof motion | wc -l`
if [[ $MotionStarted -eq 1 ]]; then
echo Motion web interface failed. Restarting motion
killall -9 motion
echo "HTTP interface has failed. killing motion and restarting" | mail -s 'Motion killed'
/etc/init.d/motion restart
else
echo Motion not started.
echo Starting Motion
/etc/init.d/motion start
fi
fi
else
echo "Status file not available"
exit
fi

for ip in "${people[@]}"; do
PersonAround=`ping -c 1 $ip | grep '64 bytes' | wc -l`
if [[ PersonAround -eq 1 ]]; then
PeopleAround=1
break
fi
done

if [[ $PeopleAround -eq 1 ]]; then
if [[ "$MotionStarted" =~ "ACTIVE" ]]; then
wget -O /usr/scripts/pause http://127.0.0.1:8080/0/detection/pause
echo "People around, stopping motion" | mail -s 'Motion stopped'
echo "People around, stopping motion"
fi
else
if [[ "$MotionStarted" =~ "PAUSE" ]]; then
wget -O /usr/scripts/start http://127.0.0.1:8080/0/detection/start
echo "No One around, Starting motion" | mail -s 'Motion started'
echo "No One around, Starting motion"
fi
fi
if [ -e /usr/scripts/status ]; then
rm /usr/scripts/status
fi

Share
Jan 202013
 

I have been playing around with Motion on one of my Linux boxes, and got annoyed at the constant stream of emails that I am receiving as I have setup Motion to send an email to myself when it detects motion, so I wrote up a short script to activate or deactivate motion if it can pick up certain devices by pinging them.

It’s not foolproof as my wifi is playing up a bit and some devices do not respond by ping sometimes, but it’s useful for deactivating something when you approach and reactivating it when you leave, or vice versa depending on your use of this script.
Setting this script up as a cronjob will allow it to run every say 5 minutes for automation.


#!/bin/bash
PeopleAround=0
people=( 192.168.2.72 192.168.2.117 )
MotionStarted=`wget -o getlog -O /usr/scripts/status http://localhost:8080/0/detection/status | cat /usr/scripts/status | sed -e 's/<[^>]*>//g' | tail -n 3 | head -n -2`

for ip in "${people[@]}"; do
PersonAround=`ping -c 1 $ip | grep '64 bytes' | wc -l`
if [[ PersonAround -eq 1 ]]; then
PeopleAround=1
break
fi
done

if [[ $PeopleAround -eq 1 ]]; then
if [[ "$MotionStarted" =~ "ACTIVE" ]]; then
wget -O /usr/scripts/status http://127.0.0.1:8080/0/detection/pause
echo "People around, stopping motion" $run | mail -s 'Motion stopped' YOUR@EMAIL.COM
fi
else
if [[ "$MotionStarted" =~ "PAUSE" ]]; then
wget -O /usr/scripts/status http://127.0.0.1:8080/0/detection/start
echo "No One around, Starting motion" $run | mail -s 'Motion started' YOUR@EMAIL.com
fi
fi

Share
Jan 102013
 

A while ago, I was having an issue with one of my computer’s WiFi connection not connecting correctly on startup.
Maybe the signal was too weak on startup or the WiFi adapter just wasn’t fast enough when the computer wanted it to connect, for some reason it would never connect and I would have to manually run an ifup command to make it connect.

I ended up making this script to check whether the WiFi had an IP address, and if it didn’t it would take down the WiFi adapter and bring it back up again.
Now, I never need to worry about that computer’s WiFi connection !
I was also going to use this for one of my Raspberry Pi projects but never got around to putting the script onto the Pi, so anyone who has the issue where hostapd breaks the IP address, this script is perfect for it !


#!/bin/bash

wlan=`/sbin/ifconfig wlan0 | grep inet\ addr | wc -l`
if [ $wlan -eq 0 ]; then
/sbin/ifdown wlan0 && /sbin/ifup wlan0
else
echo interface is up
fi

I’ve setup this script to run as a cronjob every 5 minutes to make sure the WiFi stays up.

Share
Apr 202012
 

Update – a newer version has been created here.

With the recent release of the Raspberry Pi, a computer designed to make coding and programming easy, fun, and accessible to children of all ages, there has been some discussion on how to change the memory split between the GPU and CPU.
I have come up with a script to make changing the split easy for all the future programmers who aren’t quite comfortable doing it themselves. I’m not a great coder so if you spot any flaws in this please shout out.

You can download the bash script here.
Feedbask is most welcome as I’m looking to improve the script.

Below is the code for the script itself if you want to copy / paste it instead.
If anyone has any suggestions for the script itself or would like some more options, feel free to express yourself in either the comments section on this port or in this thread


#!/bin/bash
##
## Raspberry Pi Memory Split Selector Script
## Author: SirLagz
## Website: http://sirlagz.net
##
## The purpose of this script is to make selecting the memory split
## on the RPi easy.
## Simply make this script executable if it's not already, move
## it to the directory with the elf files, and run it with ./select.sh
## The menu should be pretty self explanatory.
##
clear
list=./*
b128det=0
b192det=0
b224det=0
bdefdet=0

for i in $list
do
case $i in
"./arm128_start.elf") b128det=1;;
"./arm192_start.elf") b192det=1;;
"./arm224_start.elf") b224det=1;;
"./start.elf") bdefdet=1;;
esac
done

if [[ "$b192det" == "$bdefdet" ]] ; then
diffresult=`diff arm192_start.elf start.elf`
if [[ -z $diffresult ]] ; then
mv arm192_start.elf arm192_start.old.elf
b192det=0
fi
fi

if [[ "$b128det" == "$bdefdet" ]] ; then
diffresult=`diff arm128_start.elf start.elf`
if [[ -z $diffresult ]] ; then
mv arm128_start.elf arm128_start.old.elf
b128det=0
fi
fi

if [[ "$b224det" == "$bdefdet" ]] ; then
diffresult=`diff arm224_start.elf start.elf`
if [[ -z $diffresult ]] ; then
mv arm224_start.elf arm224_start.old.elf
b224det=0
fi
fi

if [[ "$b128det" == 0 ]] ; then
current=128
elif [[ "$b192det" == 0 ]] ; then
current=192
elif [[ "$b224det" == 0 ]] ; then
current=224
fi

declare -i vram
vram=256-$current

echo "##################################"
echo "## Raspberry Pi Memory ##"
echo "## Selector Script ##"
echo "##################################"
echo " Current Memory Split"
echo " CPU $current/$vram VRAM"
echo "##################################"
case $current in
128)
echo "1) Set CPU/VRAM split to 192/64"
echo "2) Set CPU/VRAM split to 224/32"
echo "3) Quit"
;;
192)
echo "1) Set CPU/VRAM split to 128/128"
echo "2) Set CPU/VRAM split to 224/32"
echo "3) Quit"
;;
224)
echo "1) Set CPU/VRAM split to 128/128"
echo "2) Set CPU/VRAM split to 192/64"
echo "3) Quit"
;;
*)
echo "Invalid memory detected"
;;
esac
echo "Enter Choice:";
read x

case $x in
1)
case $current in
128)
mv start.elf arm128_start.elf && mv arm192_start.elf start.elf
;;
192)
mv start.elf arm192_start.elf && mv arm128_start.elf start.elf
;;
224)
mv start.elf arm224_start.elf && mv arm128_start.elf start.elf
;;
*)
echo "Invalid memory detected"
;;
esac
;;
2)
case $current in
128)
mv start.elf arm128_start.elf && mv arm224_start.elf start.elf
;;
192)
mv start.elf arm192_start.elf && mv arm224_start.elf start.elf
;;
224)
mv start.elf arm224_start.elf && mv arm192_start.elf start.elf
;;
*)
echo "Invalid memory detected"
;;
esac
;;
3) exit 0;;
esac
if [[ $? -ne 0 ]]; then
echo "Memory Split setting failed"
fi

Share