cjdns.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. $CJDROUTE < $CONF &>> $LOGTO
  66. if [ $? -gt 0 ]; then
  67. echo "Failed to start cjdns"
  68. return 1
  69. fi
  70. else
  71. echo "cjdns is already running"
  72. return 1
  73. fi
  74. }
  75. status()
  76. {
  77. echo -n "* cjdns is "
  78. if [ -z "$PID" ]; then
  79. echo "not running"
  80. exit 1
  81. else
  82. echo "running"
  83. exit 0
  84. fi
  85. }
  86. update()
  87. {
  88. if [ -d ${CJDPATH}/cjdns/.git ]; then
  89. cd ${CJDPATH}/cjdns
  90. git pull
  91. ./do || echo "Failed to update!" && exit 1
  92. echo "* Update complete, restarting cjdns"
  93. stop
  94. load_pid
  95. start
  96. else
  97. echo "The cjdns source directory does not exist"
  98. return 1
  99. fi
  100. }
  101. case "$1" in
  102. "start" )
  103. start
  104. ;;
  105. "restart" )
  106. stop
  107. load_pid
  108. start
  109. ;;
  110. "stop" )
  111. stop
  112. ;;
  113. "status" )
  114. status
  115. ;;
  116. "check" )
  117. ps aux | grep -v 'grep' | grep 'cjdns core' > /dev/null 2>/dev/null || start
  118. ;;
  119. "update" )
  120. update
  121. ;;
  122. *)
  123. echo "usage: $0 {start|stop|restart|check|update}"
  124. esac