kurento-demo.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: ${project.artifactId}
  4. # Required-Start: $remote_fs $network
  5. # Required-Stop: $remote_fs
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: ${project.artifactId} daemon.
  9. # Description: Kurento Demo project
  10. # processname:
  11. ### END INIT INFO
  12. if [ -r "/lib/lsb/init-functions" ]; then
  13. . /lib/lsb/init-functions
  14. else
  15. echo "E: /lib/lsb/init-functions not found, package lsb-base needed"
  16. exit 1
  17. fi
  18. SERVICE_NAME=${project.artifactId}
  19. # Find out local or system installation
  20. DEMO_DIR=$(cd $(dirname $(dirname $0)); pwd)
  21. if [ -f $DEMO_DIR/bin/start.sh -a -f $DEMO_DIR/lib/${project.artifactId}.jar ]; then
  22. DEMO_SCRIPT=$DEMO_DIR/bin/start.sh
  23. CONSOLE_LOG=$DEMO_DIR/logs/${project.artifactId}.log
  24. DEMO_CONFIG=$DEMO_DIR/config/${project.artifactId}.conf.json
  25. PIDFILE=$DEMO_DIR/${project.artifactId}.pid
  26. else
  27. # Only root can start ${project.artifactId} in system mode
  28. if [ `id -u` -ne 0 ]; then
  29. log_failure_msg "Only root can start ${project.artifactId}"
  30. exit 1
  31. fi
  32. [ -f /etc/default/${project.artifactId} ] && . /etc/default/${project.artifactId}
  33. DEMO_SCRIPT=/usr/bin/${project.artifactId}
  34. CONSOLE_LOG=/var/log/kurento-media-server/${project.artifactId}.log
  35. DEMO_CONFIG=/etc/kurento/${project.artifactId}.conf.json
  36. DEMO_CHUID="--chuid $DAEMON_USER"
  37. PIDFILE=/var/run/kurento/${project.artifactId}.pid
  38. fi
  39. [ -z "$DAEMON_USER" ] && DAEMON_USER=nobody
  40. # Check startup file
  41. if [ ! -x $DEMO_SCRIPT ]; then
  42. log_failure_msg "$DEMO_SCRIPT is not an executable!"
  43. exit 1
  44. fi
  45. # Check config file
  46. # if [ ! -f $DEMO_CONFIG ]; then
  47. # log_failure_msg "${project.artifactId} configuration file not found: $DEMO_CONFIG"
  48. # exit 1;
  49. #fi
  50. # Check log directory
  51. [ -d $(dirname $CONSOLE_LOG) ] || mkdir -p $(dirname $CONSOLE_LOG)
  52. start() {
  53. log_daemon_msg "$SERVICE_NAME starting"
  54. # clean PIDFILE before start
  55. if [ -f "$PIDFILE" ]; then
  56. if [ -n "$(ps h --pid $(cat $PIDFILE) | awk '{print $1}')" ]; then
  57. log_action_msg "$SERVICE_NAME is already running ..."
  58. return
  59. fi
  60. rm -f $PIDFILE
  61. fi
  62. # DEMO instances not identified => Kill them all
  63. CURRENT_DEMO=$(ps -ef|grep ${project.artifactId}.jar |grep -v grep | awk '{print $2}')
  64. [ -n "$CURRENT_DEMO" ] && kill -9 $CURRENT_DEMO > /dev/null 2>&1
  65. mkdir -p $(dirname $PIDFILE)
  66. mkdir -p $(dirname $CONSOLE_LOG)
  67. cat /dev/null > $CONSOLE_LOG
  68. # Start daemon
  69. start-stop-daemon --start $DEMO_CHUID \
  70. --make-pidfile --pidfile $PIDFILE \
  71. --background --no-close \
  72. --exec "$DEMO_SCRIPT" -- >> $CONSOLE_LOG 2>&1
  73. log_end_msg $?
  74. }
  75. stop () {
  76. if [ -f $PIDFILE ]; then
  77. read kpid < $PIDFILE
  78. kwait=15
  79. count=0
  80. log_daemon_msg "$SERVICE_NAME stopping ..."
  81. kill -15 $kpid
  82. until [ `ps --pid $kpid 2> /dev/null | grep -c $kpid 2> /dev/null` -eq '0' ] || [ $count -gt $kwait ]
  83. do
  84. sleep 1
  85. count=$((count+1))
  86. done
  87. if [ $count -gt $kwait ]; then
  88. kill -9 $kpid
  89. fi
  90. rm -f $PIDFILE
  91. log_end_msg $?
  92. else
  93. log_failure_msg "$SERVICE_NAME is not running ..."
  94. fi
  95. }
  96. status() {
  97. if [ -f $PIDFILE ]; then
  98. read ppid < $PIDFILE
  99. if [ `ps --pid $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
  100. log_daemon_msg "$prog is running (pid $ppid)"
  101. return 0
  102. else
  103. log_daemon_msg "$prog dead but pid file exists"
  104. return 1
  105. fi
  106. fi
  107. log_daemon_msg "$SERVICE_NAME is not running"
  108. return 3
  109. }
  110. case "$1" in
  111. start)
  112. start
  113. ;;
  114. stop)
  115. stop
  116. ;;
  117. restart)
  118. $0 stop
  119. $0 start
  120. ;;
  121. status)
  122. status
  123. ;;
  124. *)
  125. ## If no parameters are given, print which are avaiable.
  126. log_daemon_msg "Usage: $0 {start|stop|status|restart|reload}"
  127. ;;
  128. esac