How to run PHP's build in server in the background - php

I upgraded to PHP 5.5, and I can't run the server in the background anymore.
ablock#desktop:~/site$ php -S localhost:3000 -t public/ &
[1] 9689
ablock#desktop:~/site$
[1]+ Stopped php -S localhost:3000 -t public/
ablock#desktop:~/site$
As you can see, the server stops right away.

When a process is set to run in the background (using the & operator) it can no longer write to the terminal. A SIGTTOU signal is generated and it's default action is to terminate the process since it no longer is able to write to stdout.
By redirecting stdout somewhere writable we can make sure that there will be no SIGTTOU signal and thous no termination of the process.
php -S localhost:3000 -t public/ 1>/dev/null &
1> means stdout, while 2> means stderr, used for errors. Both can be redirected to a file or a pseudo-devices using &>.

Related

Tailing named pipe in Docker to write to stdout

My Dockerfile:
FROM php:7.0-fpm
# Install dependencies, etc
RUN \
&& mkfifo /tmp/stdout \
&& chmod 777 /tmp/stdout
ADD docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
as you can see I'm creating a named pipe at /tmp/stdout. And my docker-entrypoint.sh:
#!/usr/bin/env bash
# Some run-time configuration stuff...
exec "./my-app" "$#" | tail -f /tmp/stdout
My PHP application (an executable named my-app) writes its application logs to /tmp/stdout. I want those logs to then be captured by Docker so that I can do docker logs <container_id> and see the logs that the application wrote to /tmp/stdout. I am attempting to do this by running the my-app command and then tailing /tmp/stdout, which will then output the logs to stdout.
What I'm seeing happen is that when I run my application, it hangs when it writes the first log message. I believe this happens because there is nothing "reading" from the named pipe, and writing to a named pipe blocks until something reads from it. This is confirmed if I do docker exec -it <container_id> bash, and then do tail -f /tmp/stdout myself inside the container. Once I do that, the container immediately exits because the application has written its logs to the named pipe.
For reasons that I won't bloat this post with, it's not possible for my-app itself to write logs to stdout. It has to use /tmp/stdout.
Can anybody tell me why this isn't working, and what I need to change? I expect I have to change the exec call in docker-entrypoint.sh, but I'm not sure how. Thank you!
What I'm seeing happen is that when I run my application, it hangs when it writes the first log message. I believe this happens because there is nothing "reading" from the named pipe, and writing to a named pipe blocks until something reads from it.
This is correct, see fifo(7). But with your example code
exec "./my-app" "$#" | tail -f /tmp/stdout
this should actually work since the pipe will start ./my-app and tail simultaneously so that there is something reading from /tmp/stdout.
But one problem here is that tail -f will never terminate by itself and so neither your docker-entrypoint.sh/container. You could fix this with:
tail --pid=$$ -f /tmp/stdout &
exec "./my-app" "$#"
tail --pid will terminate as soon as the process provided by id terminates where $$ is the pid of the bash process (and through exec later the pid of ./my-app).
For reasons that I won't bloat this post with, it's not possible for my-app itself to write logs to stdout. It has to use /tmp/stdout.
Does this mean it has to write to a filesystem path or is the path /tmp/stdout hardcoded?
If you can use any path you can use /dev/stdout / /proc/self/fd/1 / /proc/$$/fd/1 as logging path to let your application write to stdout.
If /tmp/stdout is hardcoded try symlinking it to /dev/stdout:
ln -s /dev/stdout /tmp/stdout

PHP script hangs when calling external command from CLI

I have a PHP script that executes an external bash script to make an SSH connection but even though i am using ssh's move to background (-f) as well as an '&' my PHP script hangs.
Problem line in PHP script
system('/usr/local/bin/startProxy 172.16.0.5 9051');
I have also tried :
system('/usr/local/bin/startProxy 172.16.0.5 9051 &');
And the startProxy script is simply :
#!/bin/bash
#
# startProxy <IP_Address> <Proxy_Port>
#
# Starts an ssh proxy connection using -D <port> to remote system
#
ssh -o ConnectTimeout=5 -f -N -D $2 $1 &
Calling the startProxy script from command line works find and the script returns immediately.
When the system() command is run, the called script does run (I can see ssh running via ps), it just never appears to return to the PHP script.
I have run into the same issue when trying to call ssh directly via the system() method as well.
Thanks #Martin,
For future self and others, I had to change the system call to
system('/usr/local/bin/startProxy 172.16.0.5 9051 2>&1 >/dev/null');
and to change the startProxy script to :
ssh -o ConnectTimeout=5 -f -N -D $2 $1 2>&1 >/dev/null
before PHP return to the rest of the script!
Anyone able to explain why PHP stops like this if output is not redirected (I presume PHP isn't scanning the command and seeing the redirection part) and also why PHP hangs is i don't include the redirection in the ssh command within startProxy, dispite the fact that both the PHP system call and the ssh command in startProxy where using background options (-f and '&' )

How can I load directory after run local server in terminal?

when I write cd project in my terminal then I get this line:
MacBook-Pro:project work$
I run my local server like this
MacBook-Pro:project work$ php -S 127.0.0.1:8000 -t public
After that I see this:
PHP 7.2.6 Development Server started at Tue May 29 10:45:40 2018
Listening on http://127.0.0.1:8000
Document root is /Users/work/project/public
Press Ctrl-C to quit.
But then I want to go back to my project folder MacBook-Pro:project work$ but when I write cd oder cd project nothing happens. Only when I press Ctr-C then this line MacBook-Pro:project work$appears again. Do I really have to quit my server to go into my project folder?
You should be able to run
php -S 127.0.0.1:8000 -t public &
in your terminal. & will set the task to be run in the background. Any output the command produces will be output to your terminal though.
To stop the command (Ctrl-C in this case) you first need to fg in your terminal to get it to the foreground, then Ctrl-C to quit.
If you're running the server in the foreground, then it is responsible for handling all input. You don't have direct access to the shell.
If you don't want that, then open another terminal, or run the server in the background, or use a multiplexer like screen or tmux.

Start as background process PHP built in server on Windows

I am using PHP built in server for testing and I was wondering is there a way you can hide cmd window when launching built in server using command php -S 127.0.0.1:809 -t Folder
I am currently working on Windows 10 so I need a Win solution.
not hundred percent sure on this, but you might try this one from:
What is cmd's equivalent to Bash's & (ampersand) for running a command without waiting for it to terminate?
so yours could be something like:
start /B php -S 127.0.0.1:809 -t Folder
You can create vbs script (run.vbs) and you can put this code in it
Dim oShell
Set oShell = WScript.CreateObject ("WSCript.shell")
oShell.run "cmd /C CD resource\php && php -S 127.0.0.1:809 -t HTML", 0
Set oShell = Nothing
0 in line signal for not displaying command line window.

How to shutdown default PHP server "php -S"

I can start the default PHP server with
php -S localhost:8000 but how to stop the server? Usually with CTRL + C yes, but if i want to do it from another terminal?
starting:
nohup php -S localhost:8000 &
pid=$!
echo $pid >/var/run/php.pid
stopping
pid=$(cat /var/run/php.pid)
kill $pid
That's only an example without proper error handling
Find the process ID, and issue a kill command. On Ubuntu linux, you can use "top" command line utility to see and stop running processes.
Once you start top, use "L" to search for "php", and you'll se it's process id
Open terminal, run htop. Press / to search and type in php -S or localhost or whatever command you've entered, F3 to search for next references if needed.
Press F9 to kill the process.

Categories