
PANIC_COUNTER_FILE="/var/lib/panic_counter"
PANIC_REBOOT_DELAY_FACTOR=10
PANIC_REBOOT_DELAY_MAX=3600  # reboot at least once an hour in case of panic


panic_action() {
    # read counter from file
    panic_counter=$(cat ${PANIC_COUNTER_FILE} 2>/dev/null || echo 0)
    
    # write increased counter back to file
    echo $((panic_counter + 1)) > ${PANIC_COUNTER_FILE}
    
    delay=$((PANIC_REBOOT_DELAY_FACTOR * panic_counter))
    if [[ "${delay}" -gt "${PANIC_REBOOT_DELAY_MAX}" ]]; then
        delay=${PANIC_REBOOT_DELAY_MAX}
    fi
    
    if [[ "${delay}" -gt 0 ]]; then
        logger -t panic -s "rebooting in ${delay} seconds (caused by $1)"
        sleep ${delay}
    fi

    logger -t panic -s "rebooting (caused by $1)"
    /sbin/reboot
}

