4

How can I serve and open the website from the current directory in one command with php built-in webserver?

The command used for php built-in webserver is:

php [options] -S <addr>:<port> [-t docroot]

However this is a running command, so the following command does not work:

php -S 127.0.0.1:8000 && open 127.0.0.1:8000

Purpose is creating a single alias command to open the website in a browser directly after starting the webserver (all from a single command):

alias lserve="php -S 127.0.0.1:8000 && open 127.0.0.1:8000"
dimitrieh
  • 401
  • 3
  • 17
  • Put it to the background? `nohup` ? – Jan Jan 05 '16 at 16:22
  • what is your desired action to `open`? accessing the server works when you e.g. run `lynx 127.0.0.1:8000`. as `open` just starts a program on a new vt which program is then called? – Marc Bredt Jan 05 '16 at 16:33
  • you can e.g. run `(php -S 127.0.0.1:8000 &); lynx 127.0.0.1:8000` which starts the srv in the background via a subshell and then the lynx cmd is called connecting to the host just setup. – Marc Bredt Jan 05 '16 at 16:40
  • The desired action is to open the website in a browser directly after starting the webserver – dimitrieh Jan 05 '16 at 21:00

2 Answers2

2

Run the server in background:

php -S 127.0.0.1:8000 & open 127.0.0.1:8000

Note that I'm using just a single & which starts a job in background. This is not related to the logical and operator &&. Bash's syntax does not allow the command that follows the & to be separated by a ;

However, there is still a problem with that solution. Since the server runs in background, you cannot close both the browser and the server with a single ^C. To achieve that you need to start both commands in a sub shell:

(trap 'kill 0' SIGINT; php -S 127.0.0.1:8000 & open http://127.0.0.1:8000)

Now you can put that into an alias:

alias lserve="(trap 'kill 0' SIGINT; php -S 127.0.0.1:8000 & open http://127.0.0.1:8000)"
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Now the question is, how to close the running php server in the same ^C – dimitrieh Jan 06 '16 at 14:25
  • I've answered that already - use a subshell. Btw, what is that `open` command doing? Is it a shell script? – hek2mgl Jan 06 '16 at 14:33
  • @user2307317 I've added the `trap` command. Using it, all processes get closed together once you press `^C`. Check this: http://stackoverflow.com/questions/8363519/how-do-i-terminate-all-the-subshell-processes – hek2mgl Jan 07 '16 at 15:08
  • this does not work for me, as the php server keeps running in the background. As soon as you refresh the page, it restarts the process in the terminal. – dimitrieh Jan 08 '16 at 14:29
  • Can't reproduce that. Why should a "page refresh" trigger a terminal application to run? Looks like there is an orphaned process still running. – hek2mgl Jan 08 '16 at 14:32
  • let me state it simpler: Open activity monitor; search for php; nothing there. Paste (trap 'kill 0' SIGINT; php -S 127.0.0.1:8000 & open http://127.0.0.1:8000); now there is something in activity monitor running under php; ^C in terminal; php still running in activity monitor. when the page which was opened due to the command is refreshed, the terminal reflects and reinstates the original command as never discontinued. – dimitrieh Jan 08 '16 at 19:20
  • I guess `the terminal reflects and reinstates the original command as never discontinued.` means just the process is still running in background and, since it was not detached from the terminal, produces output in the terminal. However, I can't reproduce that. Are you using Linux? – hek2mgl Jan 08 '16 at 19:32
  • strange.. running iterm2 with zsh on osx latest installment – dimitrieh Jan 08 '16 at 21:12
  • Can we find a solution to this together with [PACT](https://github.com/nickodell/pact) ? – dimitrieh Jan 22 '16 at 11:26
  • Great:) will await your take on it! – dimitrieh Jan 22 '16 at 11:47
  • @strages Propbably we should not use the `open` utility. we should use the browser directly. I'm working on Linux, I can't reproduce how it works (Well I read the man page, and it looks like it will return immediately after starting the browser) – hek2mgl Jan 23 '16 at 10:57
0

In the sake of helping someone passing here:

chromium-browser-app=http://127.0.0.1:8000 | php -S 127.0.0.1:8000

NVRM
  • 11,480
  • 1
  • 88
  • 87