shlink-daemon.sh 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: shlink_swoole
  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: Shlink non-blocking server with swoole
  9. ### END INIT INFO
  10. SCRIPT=/path/to/shlink/vendor/bin/mezzio-swoole\ start
  11. RUNAS=root
  12. PIDFILE=/var/run/shlink_swoole.pid
  13. LOGDIR=/var/log/shlink
  14. LOGFILE=${LOGDIR}/shlink_swoole.log
  15. start() {
  16. if [[ -f "$PIDFILE" ]] && kill -0 $(cat "$PIDFILE"); then
  17. echo 'Shlink with swoole already running' >&2
  18. return 1
  19. fi
  20. echo 'Starting shlink with swoole' >&2
  21. mkdir -p "$LOGDIR"
  22. touch "$LOGFILE"
  23. local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
  24. su -c "$CMD" $RUNAS > "$PIDFILE"
  25. echo 'Shlink started' >&2
  26. }
  27. stop() {
  28. if [[ ! -f "$PIDFILE" ]] || ! kill -0 $(cat "$PIDFILE"); then
  29. echo 'Shlink with swoole not running' >&2
  30. return 1
  31. fi
  32. echo 'Stopping shlink with swoole' >&2
  33. kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
  34. echo 'Shlink stopped' >&2
  35. }
  36. case "$1" in
  37. start)
  38. start
  39. ;;
  40. stop)
  41. stop
  42. ;;
  43. restart)
  44. stop
  45. start
  46. ;;
  47. *)
  48. echo "Usage: $0 {start|stop|restart}"
  49. esac