bootandkill-servers 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env bash
  2. # How to run:
  3. #
  4. # ./bootandkill-servers ./test-acceptance
  5. #
  6. # this will boot hexo & uppy-server, run the script that you provided as an argument,
  7. # and tear down the servers
  8. set -o pipefail
  9. set -o errexit
  10. set -o nounset
  11. # set -o xtrace
  12. # Set magic variables for current file & dir
  13. __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  14. __file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
  15. __base="$(basename ${__file} .sh)"
  16. __root="$(cd "$(dirname "${__dir}")" && pwd)"
  17. if [ ! -f "${__root}/env.sh" ]; then
  18. cp "${__root}/env.example.sh" "${__root}/env.sh"
  19. fi
  20. if [ "${UPPYSERVER_DROPBOX_KEY:-}" = "" ] || [ "${UPPYSERVER_DROPBOX_KEY:-***}" = "***" ]; then
  21. source "${__root}/env.sh"
  22. fi
  23. if [ "${UPPYSERVER_DROPBOX_KEY:-}" = "" ] || [ "${UPPYSERVER_DROPBOX_KEY:-***}" = "***" ]; then
  24. echo "[${__base}] Env var UPPYSERVER_DROPBOX_KEY still had the example value '${UPPYSERVER_DROPBOX_KEY:-}'. "
  25. echo "[${__base}] Please save the actual secrets in '${__root}/env.sh' and try again"
  26. exit 1
  27. fi
  28. function killProcessListeningOnPort () {
  29. local port="${1}"
  30. lsof -n -i4TCP:${port} | awk '/LISTEN/ {print $2}' |xargs kill -9
  31. }
  32. function cleanup_servers () {
  33. echo "[${__base}] --> Killing any server listening on port 4000"
  34. killProcessListeningOnPort 4000 || true
  35. echo "[${__base}] --> Killing any server listening on port 8080"
  36. killProcessListeningOnPort 8080 || true
  37. kill -9 ${tailPid}
  38. }
  39. echo "[${__base}] --> Killing any server listening on port 4000"
  40. killProcessListeningOnPort 4000 || true
  41. echo "[${__base}] --> Killing any server listening on port 8080"
  42. killProcessListeningOnPort 8080 || true
  43. echo "[${__base}] --> Start webserver and uppy-server in the background"
  44. rm -f nohup.out || true
  45. touch nohup.out
  46. nohup npm run start &
  47. tail -f nohup.out &
  48. tailPid=${!}
  49. trap cleanup_servers EXIT
  50. ${@}