5151 exit 1
5252fi
5353
54- # Include operation system function to for example detect Windows.
54+ # Include operation system function to for example detect Windows like isWindows .
5555source " ${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.
5858source " ${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
6161isDatabaseReady=$( isDatabaseQueryable)
62- if [[ ${isDatabaseReady} == " false" ] ]; then
62+ if [ " ${isDatabaseReady} " = " false" ]; then
6363 echo " stopNeo4j: ${neo4j_directory} already stopped"
6464 exit 0
6565else
7171 fi
7272fi
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
7575isDatabaseReady=$( isDatabaseQueryable)
76- if [[ ${isDatabaseReady} == " false" ] ]; then
76+ if [ " ${isDatabaseReady} " = " false" ]; then
7777 echo " stopNeo4j: Successfully stopped ${neo4j_directory} "
7878else
7979 if ! isWindows; then
8686if isWindows; then
8787 echo " stopNeo4j: Skipping detection of processes listening to port ${NEO4J_HTTP_PORT} on Windows"
8888else
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+ # Display process info
93+ echo " ${port_listener_process_ids} " | tr ' \n' ' ,' | sed ' s/,$//' | xargs -I {} ps -p {} || true
94+ # Try graceful shutdown first with SIGTERM on each PID
95+ echo " ${port_listener_process_ids} " | while read -r pid; do
96+ [ -n " ${pid} " ] && kill -TERM " ${pid} " 2> /dev/null || true
97+ done
98+ sleep 20
99+ # Check if process(es) are still alive and force kill if necessary
100+ echo " ${port_listener_process_ids} " | while read -r pid; do
101+ [ -n " ${pid} " ] && kill -0 " ${pid} " 2> /dev/null && {
102+ echo " stopNeo4j: Process ${pid} still alive after SIGTERM — sending SIGKILL"
103+ kill -KILL " ${pid} " 2> /dev/null || true
104+ } || true
105+ done
106+ else
107+ echo " stopNeo4j: No processes listening to port ${NEO4J_HTTP_PORT} detected. Assuming Neo4j is stopped."
95108 fi
96109fi
0 commit comments