watchdog.sh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/bin/bash
  2. set -e
  3. version=1.3
  4. if [ -z $1 ];then mode="0";else mode="1";fi
  5. function print_centered {
  6. [[ $# == 0 ]] && return 1
  7. declare -i TERM_COLS="$(tput cols)"
  8. declare -i str_len="${#1}"
  9. [[ $str_len -ge $TERM_COLS ]] && {
  10. echo "$1";
  11. return 0;
  12. }
  13. declare -i filler_len="$(( (TERM_COLS - str_len) / 2 ))"
  14. [[ $# -ge 2 ]] && ch="${2:0:1}" || ch=" "
  15. filler=""
  16. for (( i = 0; i < filler_len; i++ )); do
  17. filler="${filler}${ch}"
  18. done
  19. printf "%s%s%s" "$filler" "$1" "$filler"
  20. [[ $(( (TERM_COLS - str_len) % 2 )) -ne 0 ]] && printf "%s" "${ch}"
  21. printf "\n"
  22. return 0
  23. }
  24. if [ $mode = "0" ]
  25. then
  26. print_centered "$(tput setaf 9)"
  27. print_centered " _ _ _ _ _ ____ "
  28. print_centered "| | | |___| |_ ___| |_| \ ___ ___ "
  29. print_centered "| | | | .'| _| _| | | | . | . |"
  30. print_centered "|_____|__,|_| |___|_|_|____/|___|_ |"
  31. print_centered " |___|"
  32. echo -n "$(tput sgr0)"
  33. print_centered "Watchdog hardware v$version"
  34. fi
  35. if [ -f /tmp/watchdog.1 ];then rm /tmp/watchdog.1;fi
  36. cat <<'EOF'>> /tmp/watchdog.1
  37. #Watchdog
  38. SUBSYSTEM=="tty", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", SYMLINK+="watchdog" , RUN="/usr/local/bin/wd_control.sh"
  39. EOF
  40. if [ -f /tmp/watchdog.2 ];then rm /tmp/watchdog.2;fi
  41. cat <<'EOF'>> /tmp/watchdog.2
  42. #!/bin/bash
  43. timing=18
  44. stty -F /dev/watchdog ispeed 9600 ospeed 9600 -ignpar cs8 -cstopb -echo
  45. echo -n -e "\x$timing" >/dev/watchdog
  46. EOF
  47. chmod +x /tmp/watchdog.2
  48. sudo mv /tmp/watchdog.2 /usr/local/bin/wd_control.sh
  49. sudo mv /tmp/watchdog.1 /etc/udev/rules.d/10-watchdog.rules
  50. if ! grep "wd_control.sh" /etc/crontab >/dev/null
  51. then
  52. sudo cp /etc/crontab /tmp/crontab
  53. sudo chmod 777 /tmp/crontab
  54. echo "" >>/tmp/crontab
  55. echo "* * * * * root /usr/local/bin/wd_control.sh" >> /tmp/crontab
  56. sudo chmod 644 /tmp/crontab
  57. sudo mv /tmp/crontab /etc/
  58. fi
  59. sudo /etc/init.d/cron restart >/dev/null
  60. if [ $mode = "0" ]
  61. then
  62. sleep 1
  63. print_centered "Installation Done..."
  64. print_centered ""
  65. fi