Skip to content

Commit 4e02d26

Browse files
committed
Gracefully stop Neo4j before terminating it
1 parent b81b565 commit 4e02d26

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

scripts/stopNeo4j.sh

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ else
5151
exit 1
5252
fi
5353

54-
# Include operation system function to for example detect Windows.
54+
# Include operation system function to for example detect Windows like isWindows.
5555
source "${SCRIPTS_DIR}/operatingSystemFunctions.sh"
5656

57-
# Include functions to check or wait for the database to be ready
57+
# Include functions to check or wait for the database to be ready like isDatabaseQueryable.
5858
source "${SCRIPTS_DIR}/waitForNeo4jHttpFunctions.sh"
5959

6060
# Check if Neo4j is stopped (not running) using a temporary NEO4J_HOME environment variable that points to the current installation
6161
isDatabaseReady=$(isDatabaseQueryable)
62-
if [[ ${isDatabaseReady} == "false" ]]; then
62+
if [ "${isDatabaseReady}" = "false" ]; then
6363
echo "stopNeo4j: ${neo4j_directory} already stopped"
6464
exit 0
6565
else
@@ -71,9 +71,9 @@ else
7171
fi
7272
fi
7373

74-
# Check if Neo4j is still not running using a temporary NEO4J_HOME environment variable that points to the current installation
74+
#Check if Neo4j is still not running using a temporary NEO4J_HOME environment variable that points to the current installation
7575
isDatabaseReady=$(isDatabaseQueryable)
76-
if [[ ${isDatabaseReady} == "false" ]]; then
76+
if [ "${isDatabaseReady}" = "false" ]; then
7777
echo "stopNeo4j: Successfully stopped ${neo4j_directory}"
7878
else
7979
if ! isWindows; then
@@ -86,11 +86,19 @@ fi
8686
if isWindows; then
8787
echo "stopNeo4j: Skipping detection of processes listening to port ${NEO4J_HTTP_PORT} on Windows"
8888
else
89-
port_listener_process_id=$( lsof -t -i:"${NEO4J_HTTP_PORT}" -sTCP:LISTEN || true )
90-
if [ -n "${port_listener_process_id}" ]; then
91-
echo "stopNeo4j: Terminating the following process that still listens to port ${NEO4J_HTTP_PORT}"
92-
ps -p "${port_listener_process_id}"
93-
# Terminate the process that is listening to the Neo4j HTTP port
94-
kill -9 "${port_listener_process_id}"
89+
port_listener_process_ids=$( lsof -t -i:"${NEO4J_HTTP_PORT}" -sTCP:LISTEN 2>/dev/null || true )
90+
if [ -n "${port_listener_process_ids}" ]; then
91+
echo "stopNeo4j: Gracefully terminating process(es) listening to port ${NEO4J_HTTP_PORT}"
92+
ps -p "${port_listener_process_ids}"
93+
# Try graceful shutdown first with SIGTERM
94+
kill -TERM "${port_listener_process_ids}" 2>/dev/null || true
95+
sleep 20
96+
# Check if process(es) are still alive and force kill if necessary
97+
for pid in ${port_listener_process_ids}; do
98+
if kill -0 "${pid}" 2>/dev/null; then
99+
echo "stopNeo4j: Process ${pid} still alive after SIGTERM — sending SIGKILL"
100+
kill -KILL "${pid}" 2>/dev/null || true
101+
fi
102+
done
95103
fi
96104
fi

0 commit comments

Comments
 (0)