tags:

views:

62

answers:

1

i would like a single alias (ts) which starts my local development server. the script should test for an open port starting at 3000 and use the first available port. additionally, some sites require a rackup file, making -R config.ru necessary. this script should check the current directory for the config.ru file and add that to the alias if present.

currently, to start my local development environment, i run:

alias  ts="thin -R config.ru -p 3000 start"

often, i need to run several servers to test different sites, so i've created additional aliases:

alias  ts1="thin -R config.ru -p 3001 start"
A: 

Well, you could do something clever and look at what ports are already bound using netstat and a command like

netstat -anp | grep LISTEN | awk '{print $4}' | sed s/".*:"//g | sort -n -u

but if you don't care about the ugly console spam you can just keep trying ports until you get one like this

for ((port=3000; port <= 3010 ; port++)); do
   if thin -p $port start; then break; fi
done
Peter van Hardenberg
You can combine the command and the `if`: `if thin -p $port start 2>/dev/null; then break; fi`
Dennis Williamson
Nice, thanks! Updated accordingly.
Peter van Hardenberg