15

I've written a PHP CLI script that executes on a Continuous Integration environment. One of the things it does is it runs Protractor tests.

My plan was to get the built-in PHP 5.4's built-in web server to run in the background:

php -S localhost:9000 -t foo/ bar.php &

And then run protractor tests that will use localhost:9000:

protractor ./test/protractor.config.js

However, PHP's built-in web server doesn't run as a background service. I can't seem to find anything that will allow me to do this with PHP.

Can this be done? If so, how? If this is absolutely impossible, I'm open to alternative solutions.

Housni
  • 963
  • 1
  • 10
  • 23
  • Are you saying you want to daemonize the php process? If you're on a nix system you could look into init.d scripting. I haven't used php5.4's web server but if it runs a process it can be put in the background. – EdgeCaseBerg May 15 '15 at 19:31

3 Answers3

29

You could do it the same way you would run any application in the background.

nohup php -S localhost:9000 -t foo/ bar.php > phpd.log 2>&1 &

Here, nohup is used to prevent the locking of your terminal. You then need to redirect stdout (>) and stderr (2>).

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
18

Also here's the way to stop built-in php server running in the background. This is useful when you need to run the tests at some stage of CI:

# Run in background as Devon advised
nohup php -S localhost:9000 -t foo/ bar.php > phpd.log 2>&1 &
# Get last background process PID
PHP_SERVER_PID=$!

# running tests and everything...
protractor ./test/protractor.config.js

# Send SIGQUIT to php built-in server running in background to stop it
kill -3 $PHP_SERVER_PID
Franz Zieris
  • 484
  • 3
  • 11
Anton Pelykh
  • 2,274
  • 1
  • 18
  • 21
  • Great that was what im am looking for. Is there a similar sulotion for windows too ? Maybe anything that I can add in the router-script of the internal server to listen for a `shutdown` command ? – Radon8472 Oct 22 '20 at 08:47
1

You can use &> to redirect both stderr and stdout to /dev/null (noWhere).

nohup php -S 0.0.0.0:9000 -t foo/bar.php &> /dev/null &
Amir Fo
  • 5,163
  • 1
  • 43
  • 51