#!
/bin/bash
kill_process_on_port() {
local port=$1
local pid=$(lsof -ti tcp:${port})
if [ ! -z "$pid" ]; then
echo "Killing process on port $port with PID $pid"
kill -9 $pid
sleep 5 # Give some time for the process to be killed
else
echo "No process to kill on port $port"
fi
}
kill_process_and_children() {
local pid=$1
local children=$(pgrep -P $pid)
for child in $children; do
kill_process_and_children $child
done
kill -15 $pid 2>/dev/null || kill -9 $pid
}
ulimit -n 100000
echo "Cleaning up... It's normal to see No such file or directory or No process to
kill on port 18080"
while true; do
kill_process_on_port 18080
rm ./setEnv.sh
rm ./token-cache.json
# Step 1: Generate setEnv.sh and other setup commands
./download
source ./.env
# Only run jupiter-swap-api if JUPITER_URL is exactly "http://0.0.0.0:18080"
if [ "$JUPITER_URL" = "http://0.0.0.0:18080" ]; then
source ./setEnv.sh
# Check if YELLOWSTONE_URL is set and use it if available
if [ -n "$YELLOWSTONE_URL" ]; then
RUST_LOG=error ./jupiter-swap-api --rpc-url $RPC_URL -p 18080 --
yellowstone-grpc-endpoint $YELLOWSTONE_URL --expose-quote-and-simulate --allow-
circular-arbitrage --enable-new-dexes &
else
RUST_LOG=error ./jupiter-swap-api --rpc-url $RPC_URL -p 18080 --expose-
quote-and-simulate --allow-circular-arbitrage --enable-new-dexes &
fi
JUPITER_PID=$!
fi
sleep 5
# Step 4: Start your application in the background
./bot &
NPM_PID=$!
# Process monitoring and restart on crash
while true; do
if ! kill -0 $NPM_PID 2>/dev/null; then
echo "npm process crashed. Restarting..."
break
elif [ ! -z "$JUPITER_PID" ] && ! kill -0 $JUPITER_PID 2>/dev/null; then
echo "Jupiter API process crashed. Restarting..."
break
fi
sleep 5
done
echo "Restarting..."
# Restart everything: first, stop the current processes
kill_process_and_children $NPM_PID
kill_process_and_children $JUPITER_PID
echo "Restarting processes..."
done