Allow stop script to kill a port
This commit is contained in:
12
README.md
12
README.md
@@ -35,6 +35,18 @@ Stop the background server with:
|
|||||||
./stop.sh
|
./stop.sh
|
||||||
```
|
```
|
||||||
|
|
||||||
|
To stop a specific running port:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./stop.sh 5201
|
||||||
|
```
|
||||||
|
|
||||||
|
or:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
PORT=5201 ./stop.sh
|
||||||
|
```
|
||||||
|
|
||||||
If you run the server in the foreground, stop it with `Ctrl+C`.
|
If you run the server in the foreground, stop it with `Ctrl+C`.
|
||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|||||||
31
stop.sh
31
stop.sh
@@ -3,6 +3,37 @@ set -euo pipefail
|
|||||||
|
|
||||||
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
PID_FILE="$ROOT_DIR/.doclink-web.pid"
|
PID_FILE="$ROOT_DIR/.doclink-web.pid"
|
||||||
|
PORT="${1:-${PORT:-}}"
|
||||||
|
|
||||||
|
stop_by_pid() {
|
||||||
|
local pid="$1"
|
||||||
|
if [[ -z "${pid:-}" ]]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if kill -0 "$pid" 2>/dev/null; then
|
||||||
|
kill "$pid"
|
||||||
|
echo "Stopped DocLink Web process $pid"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Process $pid is not running anymore."
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
if [[ -n "${PORT:-}" ]]; then
|
||||||
|
if command -v lsof >/dev/null 2>&1; then
|
||||||
|
PIDS="$(lsof -ti tcp:"$PORT" 2>/dev/null || true)"
|
||||||
|
if [[ -n "${PIDS:-}" ]]; then
|
||||||
|
for PID in $PIDS; do
|
||||||
|
stop_by_pid "$PID"
|
||||||
|
done
|
||||||
|
rm -f "$PID_FILE"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
echo "No process found listening on port $PORT."
|
||||||
|
fi
|
||||||
|
|
||||||
if [[ ! -f "$PID_FILE" ]]; then
|
if [[ ! -f "$PID_FILE" ]]; then
|
||||||
echo "No pid file found. If the server is running in another terminal, stop it with Ctrl+C."
|
echo "No pid file found. If the server is running in another terminal, stop it with Ctrl+C."
|
||||||
|
|||||||
Reference in New Issue
Block a user