meshinstall-initd.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: <NAME>
  4. # Required-Start: $local_fs $network $named $time $syslog
  5. # Required-Stop: $local_fs $network $named $time $syslog
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Description: <DESCRIPTION>
  9. ### END INIT INFO
  10. SCRIPT=/usr/local/mesh/meshagent
  11. RUNAS=root
  12. PIDFILE=/var/run/meshagent.pid
  13. LOGFILE=/var/log/meshagent.log
  14. start() {
  15. if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE") 2>/dev/null; then
  16. echo 'Service already running' >&2
  17. return 1
  18. fi
  19. echo 'Starting service…' >&2
  20. local CMD="$SCRIPT -exec \"var child; process.on('SIGTERM', function () { child.removeAllListeners('exit'); child.kill(); process.exit(); }); function start() { child = require('child_process').execFile(process.execPath, [process.argv0, \"\"]); child.stdout.on('data', function (c) { }); child.stderr.on('data', function (c) { }); child.on('exit', function (status) { start(); }); } start();\" &> \"$LOGFILE\" & echo \$!"
  21. cd /usr/local/mesh
  22. su -c "$CMD" $RUNAS > "$PIDFILE"
  23. echo 'Service started' >&2
  24. }
  25. stop() {
  26. if [ ! -f "$PIDFILE" ]; then
  27. echo 'Service not running' >&2
  28. return 1
  29. else
  30. pid=$( cat "$PIDFILE" )
  31. if kill -0 $pid 2>/dev/null; then
  32. echo 'Stopping service…' >&2
  33. kill -15 $pid
  34. echo 'Service stopped' >&2
  35. else
  36. echo 'Service not running'
  37. fi
  38. rm -f $"PIDFILE"
  39. fi
  40. }
  41. restart(){
  42. stop
  43. start
  44. }
  45. status(){
  46. if [ -f "$PIDFILE" ]
  47. then
  48. pid=$( cat "$PIDFILE" )
  49. if kill -0 $pid 2>/dev/null; then
  50. echo "meshagent start/running, process $pid"
  51. else
  52. echo 'meshagent stop/waiting'
  53. fi
  54. else
  55. echo 'meshagent stop/waiting'
  56. fi
  57. }
  58. case "$1" in
  59. start)
  60. start
  61. ;;
  62. stop)
  63. stop
  64. ;;
  65. restart)
  66. stop
  67. start
  68. ;;
  69. status)
  70. status
  71. ;;
  72. *)
  73. echo "Usage: service meshagent {start|stop|restart|status}"
  74. ;;
  75. esac
  76. exit 0