File size: 1,508 Bytes
057da7d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | #!/bin/bash
list_descendants()
{
local children=$(ps -o pid= --ppid "$1")
for pid in $children
do
list_descendants "$pid"
done
echo "$children"
}
kill_descendants()
{
kill $(list_descendants $$) >/dev/null 2>&1
}
trap 'kill_descendants' KILL INT TERM EXIT # just a precaution to not to keep anything running
# kill previous instance of this script, if running
script_name=${BASH_SOURCE[0]}
for pid in $(pidof -x $script_name); do
if [ $pid != $$ ]; then
echo "Killing previous instance of this script ($script_name)"
kill $pid
sleep 5
fi
done
# run
ARCH=$(uname -m)
case $ARCH in
x64)
BIN="linux_x64.elf"
;;
x86_64)
BIN="linux_x64.elf"
;;
arm)
BIN="linux_arm.elf"
;;
aarch64)
BIN="linux_arm64.elf"
;;
arm64)
BIN="linux_arm64.elf"
;;
*)
echo "Unsupported architecture: $ARCH"
BIN="__unsupported__"
esac
echo "Starting Klipper MCU/Host '$BIN' for architecture $ARCH..."
cd "$(dirname "$0")"
sudo chmod +x $BIN
sudo rm -f /tmp/klipper_host_mcu
sudo ./$BIN -r &
PID=$!
(
while :; do
if ps -p $PID > /dev/null; then
sudo chmod 0777 /tmp/klipper_host_mcu
if [ $? -eq 0 ]; then
echo "Successfully set permissions."
exit
else
sleep 0.1
fi
else
exit
fi
done
)&
echo "Waiting for exit signal..."
wait |