| #!/bin/sh |
| # |
| # Runs poweroff after printing a message to the log, setting the LEDs to red |
| # to indicate a failure, and waiting for logs to upload. |
| # |
| |
| if [ "$#" = 0 ]; then |
| echo "Usage: $0 <message>" >&2 |
| exit 1 |
| fi |
| |
| exec >&2 |
| |
| echo "<1>poweroff:" "$@" >/dev/kmsg |
| if [ -e /config/NO-POWEROFF ]; then |
| echo "<1>poweroff: disabled by /config/NO-POWEROFF." >/dev/kmsg |
| exit 0 |
| fi |
| |
| # This runs just in case we encounter trouble during the next steps. |
| # This background task is double-nested so that it doesn't count toward |
| # the 'wait' command below. |
| ( |
| ( |
| sleep 30 |
| echo "Timed out; powering off immediately." |
| poweroff |
| ) & |
| ) |
| |
| echo "Waiting for LEDs and logs to sync." |
| ( |
| : >/tmp/gpio/ledcontrol/halted |
| leds-and-wait 1 # red |
| echo "LEDs set." |
| ) & |
| ( |
| upload-logs-and-wait |
| echo "Logs uploaded." |
| ) & |
| wait |
| |
| echo "Powering off." |
| poweroff |