2

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?

Wouter
  • 141

2 Answers2

1

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

#!/bin/bash
node server.js &
xdg-open http://localhost:9000/
A.B.
  • 92,125
1

xdg-open is actually a shell script (on my system). It uses heuristics to guess what your browser is. You could see what it is trying to do by running it with from within the script with

sh -x xdg-open http://localhost:9000/ >&/tmp/errors

Look in /tmp/errors for any failure messages. You may also simply replace xdg-open by the name of your browser, eg firefox.

If you are launching a script from a desktop button, it may be that when the launcher gets to the end of the script, it kills the process group. Try adding a

 sleep 99999

at the end to see if this is the case.

If you are trying to run your startserver.sh from somewhere other than your desktop environment, xdg-open may have a hard time trying to find which browser to use. You could try setting some environment variables to help it.

meuh
  • 3,444