intelligent thin start with port alias for bash
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"
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
精彩评论