The problem is (probably) not that the server doesn't stay alive long enough; it's that it doesn't start up fast enough. When the open
command executes, the webserver hasn't started yet, so you get an error.
Try sleeping for a second or two before the open