-
Installing the script on your Raspberry Pi
07/19/2014 at 17:15 • 0 commentsThere are many flavors of Linux available for the Raspberry Pi, so I will focus on the generic Raspbian build as well as one for Arch Linux. Why Arch? I am using MinePeon in my Bitcoin mining rig!
Raspian:
First, install git if you haven't already:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install git-core
Download wiringPi and install it:
git clone git://git.drogon.net/wiringPi
cd wiringPi
./build
Create the shutdowncheck script:
cd ~
sudo nano shutdowncheck
Type the following into the nano text editor:
#!/bin/bash
PATH=/usr/bin:/home/pi/wiringPi/gpio
echo "Shutdown script starting..."
echo "Asserting pins (7=in, low(pull-down); 8=out,high)"
gpio -g mode 7 in
gpio -g mode 7 down
gpio -g write 7 0
gpio -g mode 8 out
gpio -g write 8 1
COUNTER=0
while [ 1 ]; do
if [ "$(/home/pi/wiringPi/gpio/gpio -g read 7)" = "1" ]; then
echo "PIN7 requested a SYSTEM HALT!"
sudo halt -p
break
fi
sudo sleep 0.5
done
exit 0After you have typed this in and double checked your work, press ctrl-o (to save) and ctrl-x (to exit).
Next, make the script executable:
sudo chmod 755 shutdowncheck
Why type? Why can't I just pull it from your github? Because you can learn a bit about how gpio works on your pi by typing it manually. Also, some distros interpret new lines differently and you may end up with a non functioning script.
Edit your /etc/rc.local file and add the following line before the last line (exit 0):
sudo nano /etc/rc.local
Add this line before exit 0:
(cd /home/pi && exec ./shutdowncheck) &
Save (ctrl-o) and exit nano (ctrl-x). Now your script will run automatically when your Pi starts up!
Arch Linux (MinePeon specific instructions)
Special thanks to synapseattack from bitcointalk.org for the MinePeon instructions!
Download wiringPi and install it:
git clone git://git.drogon.net/wiringPi
cd wiringPi
./build
sudo cp /home/minepeon/wiringPi/gpio/gpio /usr/bin/
Create the shutdowncheck script:
sudo nano /usr/bin/shutdowncheck
Type the following into nano and then hit ctrl-o (save) and then ctrl-x (exit):
#!/bin/bash
PATH=/usr/bin
echo "Shutdown script starting..."
echo "Asserting pins (7=in, low(pull-down); 8=out,high)"
gpio -g mode 7 in
gpio -g mode 7 down
gpio -g write 7 0
gpio -g mode 8 out
gpio -g write 8 1
COUNTER=0
while [ 1 ]; do
if [ "$(/usr/bin/gpio -g read 7)" = "1" ]; then
echo "PIN7 requested a SYSTEM HALT!"
sudo halt -p
break
fi
sudo sleep 0.5
done
exit 0Make the new file executable:
sudo chmod +x /usr/bin/shutdowncheck
Now make it a service:
sudo nano /usr/lib/systemd/system/shutdowncheck.service
Type this into nano (ctrl-o and ctrl-x to save and exit):
[Unit]
Description=ShutdownCheck
ConditionFileIsExecutable=/usr/bin/shutdowncheck
[Service]
Type=forking
ExecStart=/usr/bin/shutdowncheck
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.targetNow enable your new service:
sudo systemctl enable shutdowncheck.service
Now your script will run during boot (you will need to reboot after enabling the service).