forked from bmoeskau/node-kill
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkill.sh
More file actions
68 lines (59 loc) · 1.5 KB
/
kill.sh
File metadata and controls
68 lines (59 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
#
# Simple script to kill and/or restart a node server
# By Brian Moeskau
#
# Basic usage: sh kill.sh [-r] server-name
#
set -o errexit
# Message to display when invalid args are passed
function usage {
echo
echo "usage: sh kill.sh [-r] server-name"
echo
echo " -r: Automatically restart the server"
echo
}
# Parse the command line args
while getopts ":r" opt; do
case $opt in
r)
restart=1
shift
;;
\?)
usage
exit 1
esac
done
# After shifting any flags above, the server name should be the only remaining arg now:
SERVER=$1
# If no server specified exit early
if [ "$SERVER" = "" ]; then
echo
echo You must specify the filename of the server to kill
usage
exit 1
fi
# Grab the list of any existing node process ids running with the specified server name
PID=$(ps ax | grep "[n]ode $SERVER" | awk '{print $1}')
# Kill existing processes if any
if [ "$PID" = "" ]; then
echo $SERVER is not currently running
else
echo Killing $SERVER...
sudo kill -KILL $PID
fi
# Just exit on Ctrl-C, skip the error message output below
trap "exit" SIGINT SIGQUIT SIGTERM
# If the restart flag was passed in try to kick off a new instance of the server
if [ "$restart" = "1" ]; then
echo Starting $SERVER "(use Ctrl-C to exit)..."
sudo node $SERVER || {
# If we're here then some error occurred trying to start the server.
# The error message will get displayed in the terminal.
echo
echo "Could not start \"$SERVER\" -- please make sure the name and path are correct"
exit 1
}
fi