Raspberry PI Shutdown on button press

After adding a button with resistors (there are lots of other resources about that). I wanted a version using systemd.

The gpio testing script is using wiringpi.

Add this as /root/pin-shutdown.sh:

#!/bin/bash

pin=29  # pin 40 (internal: 21) on raspi layout
gpio mode $pin in
gpio wfi $pin both
logger Shutdown button pressed
shutdown -h now

gpio wfi 21 both is blocking until the pin changes its state.

And the systemd service as: /etc/systemd/system/button-shutdown.service:

[Unit]
Description=button shutdown service

[Service]
ExecStart=/root/pin-shutdown.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

Enable the systemd service with: systemctl enable button-shutdown

start script with systemd

I wanted to shoot as many photos as possible with a raspicam. But no photos at night (the hours are in UTC). Save the file as /root/shoot.sh and set the file as executable.

#!/bin/sh

mkdir /root/images

while true
do
  sleep 1
  if (( `date +%H` < 21 )); then
    if (( `date +%H` > 2 )); then
      /opt/vc/bin/raspistill -o /root/images/`date +%s`.jpg -rot 180
    fi
  fi
done

Archlinuxarm doesn't have a rc.local file. So I had to use systemd to start the script and keep it alive:

[Unit]
Description=Camera shooting service

[Service]
ExecStart=/root/shoot.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

Save as /etc/systemd/system/camera.service and enable it with systemctl enable camera.service. It will start now at the next boot.

Find active servers in subnet

I needed an org-mode table with a list of all currently used IP addresses in a subnet.

#+BEGIN_SRC bash
  nmap -sP 10.7.2.0/24 | grep "scan report" | cut -d" " -f5 | sort -V
#+END_SRC

#+RESULTS:
|   10.7.2.1 |
|   10.7.2.2 |
|  10.7.2.23 |
The nmap command returns for each server 2 lines.
The line with the IP address contains "scan report".
cut splits the IP address from the line and sort -V sorts by IP address.

To run a command in org-mode you press C-c C-c inside the region and answer the security question with y.