cjdns.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/env bash
  2. #
  3. # You may redistribute this program and/or modify it under the terms of
  4. # the GNU General Public License as published by the Free Software Foundation,
  5. # either version 3 of the License, or (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ###
  15. # cjdns.sh
  16. #
  17. # Copy this script to the desired location, then add the following to the cjdns user's crontab
  18. # */5 * * * * /path/to/cjdns.sh check >>/dev/null 2>>/dev/null
  19. #
  20. # Start cjdns if it isn't already running (and set the above cronjob to restart failed processes):
  21. # ./cjdns.sh start
  22. #
  23. # Stop cjdns if it's currently running (and set the above cronjob not to restart failed processes):
  24. # ./cjdns.sh stop
  25. #
  26. # Check whether cjdns is currently running:
  27. # ./cjdns.sh status
  28. #
  29. # Restart cjdns after upgrades and changes to the config:
  30. # ./cjdns.sh restart
  31. ##
  32. if [ -f /etc/default/cjdns ]; then
  33. . /etc/default/cjdns
  34. fi
  35. # path to the cjdns source tree, no trailing slash
  36. if [ -z "$CJDPATH" ]; then CJDPATH=`dirname $0`; fi
  37. # full path to the cjdroute binary
  38. if [ -z "$CJDROUTE" ]; then CJDROUTE="${CJDPATH}/cjdns/cjdroute"; fi
  39. # full path to the configuration file
  40. if [ -z "$CONF" ]; then CONF="${CJDPATH}/cjdroute.conf"; fi
  41. # path to the log file.
  42. if [ -z "$LOGTO" ]; then LOGTO="/dev/null"; fi
  43. load_pid()
  44. {
  45. PID=$(pgrep -d " " -f "$CJDROUTE")
  46. }
  47. load_pid
  48. stop()
  49. {
  50. if [ -z "$PID" ]; then
  51. echo "cjdns is not running"
  52. return 1
  53. else
  54. kill $PID &> /dev/null
  55. while [ -n "$(pgrep -d " " -f "$CJDROUTE")" ]; do
  56. echo "* Waiting for cjdns to shut down..."
  57. sleep 1;
  58. done
  59. if [ $? -gt 0 ]; then return 1; fi
  60. fi
  61. }
  62. start()
  63. {
  64. if [ -z "$PID" ]; then
  65. [ -e "$CONF" ] || $CJDROUTE --genconf > $CONF
  66. $CJDROUTE < $CONF &>> $LOGTO
  67. if [ $? -gt 0 ]; then
  68. echo "Failed to start cjdns"
  69. return 1
  70. fi
  71. else
  72. echo "cjdns is already running"
  73. return 1
  74. fi
  75. }
  76. status()
  77. {
  78. echo -n "* cjdns is "
  79. if [ -z "$PID" ]; then
  80. echo "not running"
  81. exit 1
  82. else
  83. echo "running"
  84. exit 0
  85. fi
  86. }
  87. update()
  88. {
  89. if [ -d ${CJDPATH}/cjdns/.git ]; then
  90. cd ${CJDPATH}/cjdns
  91. git pull
  92. ./do || echo "Failed to update!" && exit 1
  93. echo "* Update complete, restarting cjdns"
  94. stop
  95. load_pid
  96. start
  97. else
  98. echo "The cjdns source directory does not exist"
  99. return 1
  100. fi
  101. }
  102. case "$1" in
  103. "start" )
  104. start
  105. ;;
  106. "restart" )
  107. stop
  108. load_pid
  109. start
  110. ;;
  111. "stop" )
  112. stop
  113. ;;
  114. "status" )
  115. status
  116. ;;
  117. "check" )
  118. ps aux | grep -v 'grep' | grep 'cjdns core' > /dev/null 2>/dev/null || start
  119. ;;
  120. "update" )
  121. update
  122. ;;
  123. *)
  124. echo "usage: $0 {start|stop|restart|check|update}"
  125. esac