Ubuntu – How to open an URL from a bash script

bashscriptssh

I have a simple script to start a node.js webserver and open the URL in a browser. However the last part doesn't seem to work, it wont open the URL in a browser (or anywhere else).

This is the code I got so far:

#!/bin/bash
node server.js;
xdg-open http://localhost:9000/;

Everywhere I search I find the same, I have to use:

xdg-open URL

but it only seems to work while typing that in the terminal, not in my startserver.sh file. It will start the server but not open the URL, typing the code in a terminal however does seem to work.

It's confusing, why doesn't it work inside my script?

Best Answer

It's a very short answer, start node server.js as background process:

#!/bin/bash
node server.js &
xdg-open http://localhost:9000/
Related Question