@@ -29,16 +29,18 @@ cleanup() {
2929 fi
3030
3131 # Wait for Overmind to fully shut down
32- max_wait=10
32+ max_wait=30 # Increased from 10 to 30 seconds
3333 count=0
3434 while [ -S " ./.overmind.sock" ] && [ $count -lt $max_wait ]; do
3535 echo " Waiting for Overmind socket to close (attempt $count /$max_wait )..."
3636 sleep 1
3737 count=$(( count + 1 ))
3838 done
3939
40+ # Force remove the socket file if it still exists
4041 if [ -S " ./.overmind.sock" ]; then
41- echo " Warning: Overmind socket still exists after waiting"
42+ echo " Warning: Overmind socket still exists after waiting, forcefully removing..."
43+ rm -f ./.overmind.sock
4244 else
4345 echo " Overmind socket closed successfully"
4446 fi
@@ -48,8 +50,8 @@ cleanup() {
4850 if pgrep -f " postgres" > /dev/null; then
4951 echo " Attempting to stop PostgreSQL gracefully..."
5052
51- # Try to find the PostgreSQL data directory
52- PGDATA=$( ps aux | grep postgres | grep -v grep | grep -oP ' (?<= -D ).*?(?=\s)' )
53+ # Improved logic to find PostgreSQL data directory
54+ PGDATA=$( ps aux | grep postgres | grep -v grep | grep -oP ' (?<= -D ).*?(?=\s)' || true )
5355
5456 if [ -n " $PGDATA " ] && [ -d " $PGDATA " ]; then
5557 # Use pg_ctl for graceful shutdown if available
@@ -58,7 +60,7 @@ cleanup() {
5860 " ${PSQLBIN} /pg_ctl" -D " $PGDATA " stop -m smart
5961
6062 # Wait for PostgreSQL to shut down
61- max_wait=30
63+ max_wait=60 # Increased from 30 to 60 seconds
6264 count=0
6365 while pgrep -f " postgres" > /dev/null && [ $count -lt $max_wait ]; do
6466 echo " Waiting for PostgreSQL to stop (attempt $count /$max_wait )..."
@@ -77,9 +79,18 @@ cleanup() {
7779 sleep 5
7880 fi
7981 else
80- echo " Warning: Could not determine PostgreSQL data directory, using signal-based shutdown"
82+ echo " Warning: Could not determine PostgreSQL data directory, using enhanced signal-based shutdown"
83+
84+ # Enhanced shutdown with multiple signals
8185 pkill -TERM postgres || true
8286 sleep 5
87+ if pgrep -f " postgres" > /dev/null; then
88+ pkill -INT postgres || true
89+ sleep 5
90+ if pgrep -f " postgres" > /dev/null; then
91+ pkill -KILL postgres || true
92+ fi
93+ fi
8394 fi
8495 fi
8596
@@ -100,15 +111,20 @@ cleanup() {
100111 echo " Warning: Some processes are still running after graceful shutdown attempts:"
101112 ps aux | grep -E " (postgres|overmind|tmux.*postgresql)" | grep -v grep
102113
103- # Last resort: Use SIGINT before SIGKILL
104- echo " Sending SIGINT to remaining processes..."
105- pkill -INT -f " (postgres|overmind|tmux.*postgresql)" || true
114+ # More aggressive cleanup
115+ echo " Sending SIGTERM to remaining processes..."
116+ pkill -TERM -f " (postgres|overmind|tmux.*postgresql)" || true
106117 sleep 5
107118
108- # If processes still remain, use SIGKILL as a final measure
109119 if ps aux | grep -E " (postgres|overmind|tmux.*postgresql)" | grep -v grep > /dev/null; then
110- echo " Force killing remaining processes..."
111- pkill -KILL -f " (postgres|overmind|tmux.*postgresql)" || true
120+ echo " Sending SIGINT to remaining processes..."
121+ pkill -INT -f " (postgres|overmind|tmux.*postgresql)" || true
122+ sleep 5
123+
124+ if ps aux | grep -E " (postgres|overmind|tmux.*postgresql)" | grep -v grep > /dev/null; then
125+ echo " Force killing remaining processes..."
126+ pkill -KILL -f " (postgres|overmind|tmux.*postgresql)" || true
127+ fi
112128 fi
113129 fi
114130
@@ -124,7 +140,6 @@ cleanup() {
124140 echo " Cleanup completed successfully"
125141 fi
126142}
127-
128143# Set up trap for cleanup on script exit
129144trap cleanup EXIT INT TERM
130145# Function to display help
0 commit comments