4

Hi I have a bash script that needs a conditional execution of a few lines of code based on whether port 80 is already in use:

sudo git fetch origin;
sudo git checkout master;
sudo git pull;

--- if port 80 open

echo Starting Meteor;
export LC_ALL=C;
export ROOT_URL=$ROOT_URL;
sudo meteor --port 80;

--- else

echo Meteor already running;

Then as a cherry on the top since Meteor is a long running process, how do i get it to run in the background and exit the script? (I've tried nohup, &, but i have no idea what the best practice is?)

Thanks so much

Alasdair P
  • 55
  • 5

1 Answers1

7

You could use:

netstat -ln | grep ":80 "

If the return code ($?) is 0 then something is on port :80, otherwise not. So for example:

netstat -ln | grep ":80 " 2>&1 > /dev/null 
if [ $? -eq 1 ]; then   
     ... your code here 
fi
fede.evol
  • 1,918
  • 1
  • 12
  • 6
  • +1 but you'll want `grep ':80\>'` so you don't match ":8080" for instance – glenn jackman Mar 19 '14 at 15:44
  • And the 2nd part? How to get the "Your code here" to execute in background? simply "Your code here &" ? – Alasdair P Mar 19 '14 at 17:02
  • gleen: I've put a space in the string after 80 (":80 ") not to match 80xx, tried and seemed to work :) – fede.evol Mar 20 '14 at 06:43
  • Alasdair: yep just put a &. If the terminal may get closed (that is you run it manually in the shell and then close it) then nohup may be usefull since will keep the process running also in that case. – fede.evol Mar 20 '14 at 06:44