watchdog.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/bin/bash
  2. set -e
  3. version=2.0
  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. export TERM=xterm-256color
  27. print_centered "$(tput setaf 9)"
  28. print_centered " _ _ _ _ _ ____ "
  29. print_centered "| | | |___| |_ ___| |_| \ ___ ___ "
  30. print_centered "| | | | .'| _| _| | | | . | . |"
  31. print_centered "|_____|__,|_| |___|_|_|____/|___|_ |"
  32. print_centered " |___|"
  33. echo -n "$(tput sgr0)"
  34. print_centered "Watchdog hardware v$version"
  35. fi
  36. if [ -f /tmp/watchdog.1 ];then rm /tmp/watchdog.1;fi
  37. cat <<'EOF'>> /tmp/watchdog.1
  38. #Watchdog
  39. SUBSYSTEM=="tty", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", SYMLINK+="watchdog" , RUN="/usr/local/bin/wdcontrol.sh"
  40. EOF
  41. if [ -f /tmp/watchdog.2 ];then rm /tmp/watchdog.2;fi
  42. cat <<'EOF'>> /tmp/watchdog.2
  43. #!/bin/bash
  44. timing=18
  45. stty -F /dev/watchdog ispeed 9600 ospeed 9600 -ignpar cs8 -cstopb -echo
  46. echo -n -e "\x$timing" >/dev/watchdog
  47. EOF
  48. chmod +x /tmp/watchdog.2
  49. sudo mv /tmp/watchdog.2 /usr/local/bin/wdcontrol.sh
  50. sudo mv /tmp/watchdog.1 /etc/udev/rules.d/10-watchdog.rules
  51. if ! grep "wd_control.sh" /etc/crontab >/dev/null
  52. then
  53. sudo cp /etc/crontab /tmp/crontab
  54. sudo chmod 777 /tmp/crontab
  55. echo "" >>/tmp/crontab
  56. echo "* * * * * root /usr/local/bin/wdcontrol.sh" >> /tmp/crontab
  57. sudo chmod 644 /tmp/crontab
  58. sudo mv /tmp/crontab /etc/
  59. fi
  60. sudo /etc/init.d/cron restart >/dev/null
  61. if [ $mode = "0" ]
  62. then
  63. sleep 1
  64. print_centered "Installation Done..."
  65. print_centered ""
  66. fi