From 1305cf9f353e15073f302e086a557d6135aed39e Mon Sep 17 00:00:00 2001 From: Akash Askoolum Date: Fri, 12 Jun 2020 08:55:06 +0100 Subject: [PATCH 1/3] add start script Adds a start script with an option to ignore an already running instance of nginx. This will be useful when scripting the starting of a service as it can become: ``` dev-nginx start -i sbt run ``` This helps avoid the situation where you receive an error such as `ERR_CONNECTION_REFUSED` in the browser as you've forgotten to manually start nginx. --- script/start | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100755 script/start diff --git a/script/start b/script/start new file mode 100755 index 0000000..fc1aa09 --- /dev/null +++ b/script/start @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +set -e + +# colours +YELLOW='\033[1;33m' +NC='\033[0m' # no colour - reset console colour + +IGNORE_IF_ALREADY_RUNNING=false + +while test $# -gt 0; do + case "$1" in + -i|--ignore-if-already-running) + IGNORE_IF_ALREADY_RUNNING=true + shift + ;; + *) + break + ;; + esac +done + +if pgrep 'nginx' > /dev/null; then + echo "nginx already running" + if [ "$IGNORE_IF_ALREADY_RUNNING" = false ] ; then + echo -e "Did you mean ${YELLOW}dev-nginx start -i${NC} or ${YELLOW}dev-nginx restart${NC}?" + exit 1 + fi +else + echo -e "${YELLOW}Starting nginx. This requires sudo access.${NC}" + sudo nginx +fi + From 7d707918ed9dea04a24ec5917ff2869bdcbef029 Mon Sep 17 00:00:00 2001 From: Akash Askoolum Date: Fri, 12 Jun 2020 09:09:21 +0100 Subject: [PATCH 2/3] update readme --- README.md | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 754637f..26f501f 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ See [troubleshooting faq](TROUBLESHOOTING.md) Installing and running dev-nginx will start an [nginx](https://nginx.org/en/) server instance locally on your machine. -This instance will use any `*.conf` files found locally within the directory `/nginx/servers` to generate a virtual server host to proxy requests to localhost. You can locate this directory with the command `dev-nginx locate-nginx`. +This instance will use any `*.conf` files found locally within the directory `/nginx/servers` to generate a virtual server host to proxy requests to localhost. You can locate this directory with the command `dev-nginx locate`. Each project config should include http directives for proxy localhost ports and necessary SSL certificates. This is quite repetitive, so `dev-nginx` abstracts it away with the `setup-app` and `setup-cert` commands. @@ -71,10 +71,11 @@ dev-nginx COMMAND Available commands: - add-to-hosts-file - link-config -- locate-nginx -- restart-nginx +- locate +- restart - setup-app - setup-cert +- start ``` ### Commands @@ -93,20 +94,27 @@ If it does not already exist, adds an entry to `/etc/hosts` that resolves to `12 dev-nginx link-config /path/to/site.conf ``` -Symlink an existing file into nginx configuration. You'll need to restart nginx to activate it (`dev-nginx restart-nginx`). +Symlink an existing file into nginx configuration. You'll need to restart nginx to activate it (`dev-nginx restart`). -#### `locate-nginx` +#### `locate` ```bash -dev-nginx locate-nginx +dev-nginx locate ``` Locates the directory nginx is installed. -#### `restart-nginx` +#### `start` +```bash +dev-nginx start +``` + +Starts nginx. Will fail if currently running. Add `-i` to ignore if nginx is currently running. + +#### `restart` ```bash -dev-nginx restart-nginx +dev-nginx restart ``` Stops, if running, and starts nginx. From 9d868bba85ca44466996677e97bc982aa72e00a6 Mon Sep 17 00:00:00 2001 From: Akash Askoolum Date: Tue, 29 Sep 2020 09:19:59 +0100 Subject: [PATCH 3/3] rename flag and add help output --- README.md | 2 +- script/start | 26 +++++++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 26f501f..bbd6665 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,7 @@ Locates the directory nginx is installed. dev-nginx start ``` -Starts nginx. Will fail if currently running. Add `-i` to ignore if nginx is currently running. +Starts nginx. Will fail if currently running. Add `-g` to ignore if nginx is currently running. #### `restart` diff --git a/script/start b/script/start index fc1aa09..94477dd 100755 --- a/script/start +++ b/script/start @@ -6,12 +6,24 @@ set -e YELLOW='\033[1;33m' NC='\033[0m' # no colour - reset console colour -IGNORE_IF_ALREADY_RUNNING=false +GRACEFUL=false + +function printHelp() { + echo "Starts nginx. Will fail if nginx is already running." + echo " Flags:" + echo " -g | --graceful ignore nginx if it's already running" + echo " -h | --help print this message" + exit 0 +} while test $# -gt 0; do case "$1" in - -i|--ignore-if-already-running) - IGNORE_IF_ALREADY_RUNNING=true + -g|--graceful) + GRACEFUL=true + shift + ;; + -h|--help) + printHelp shift ;; *) @@ -21,13 +33,13 @@ while test $# -gt 0; do done if pgrep 'nginx' > /dev/null; then - echo "nginx already running" - if [ "$IGNORE_IF_ALREADY_RUNNING" = false ] ; then - echo -e "Did you mean ${YELLOW}dev-nginx start -i${NC} or ${YELLOW}dev-nginx restart${NC}?" + if [ "$GRACEFUL" = true ] ; then + exit 0 + else + echo -e "Error: nginx already running. Did you mean ${YELLOW}dev-nginx restart${NC} or ${YELLOW}dev-nginx start -g${NC}?" exit 1 fi else echo -e "${YELLOW}Starting nginx. This requires sudo access.${NC}" sudo nginx fi -