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

1
  • Do you mean it's making a connection to port 80 or if the computer is already listening on port 80?
    – DJ Chateau
    Mar 19, 2014 at 15:23

1 Answer 1

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
4
  • +1 but you'll want grep ':80\>' so you don't match ":8080" for instance Mar 19, 2014 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, 2014 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, 2014 at 6: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, 2014 at 6:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .