How to shutdown default PHP server "php -S" - php

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.

Related

How run PHP script file in the background forever

My issue seems to be asked before but hold on, this one is a bit different.
I have 2 php files, I run the following commands:
nohup php file_1.php >/dev/null 2>&1 &
nohup php file_2.php >/dev/null 2>&1 &
This will create basically 2 php processes.
My problem is that sometimes one of the files or even both of them are killed by the server for unknown reason and I have to re-enter the commands over again. I tried 'forever' but doesn't help.
If the server is rebooted I will have to enter those 2 commands, I thought about Cronjob but I'm not sure if it would launch it twice which would create more confusion.
My question is how to start automatically the files if one or both of them got killed? What is the best way to achieve this which would check exactly that file_1.php or that file_2.php is indeed running?
There are a couple of ways you can do this. As #Chris Haas mentioned in the comments, supervisord can do this for you, or you can run a watchdog shell script from cron at regular intervals that checks to see if your script is running, and if not, start it. Here's one I use.
#!/bin/bash
FIND_PROC=`ps -ef |grep "php queue_agent.php --env prod" | awk '{if ($8 !~ /grep/) print $2}'`
# if FIND_PROC is empty, the process has died; restart it
if [ -z "${FIND_PROC}" ]; then
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo queue_agent.php failed at `date`
cd ${DIR}
nohup nice -n 10 php queue_agent.php --env prod -v > ../../sandbox/logs/queue_agent.log &
fi
exit 0
I think u can try to figure out why these two php scripts shut down as the first step. To solve this problem, u can use this php function:
void register_shutdown_function(callback $callback[.mixed $parameter]);
which Registers a callback to be executed after script execution finishes or exit() is called. So u can log some info when php files get shut down like this:
function myLogFunction() {
//log some info here
}
register_shutdown_function(myLogFunction);
Instead of putting the standard output and error output into /dev/null, u can put them into a log file(Since maybe we can get some helpful info from the output). So instead of using:
nohup php file_1.php >/dev/null 2>&1 &
nohup php file_2.php >/dev/null 2>&1 &
try:
nohup php file_1.php 2>yourLog.log &
nohup php file_2.php 2>yourLog.log &
If u want to autorun these two php files when u boot the server, try edit /etc/rc.local(which is autorun when the os start up). Add your php cli command lines in this file.
If u can't figure out why php threads get shut down, try supervisor as #Chris Haas mensioned.

How to allow backgrounding a process to survive a session termination?

So I'm using a Putty session to run a script on background.
my script can be located by
cd /var/www/listingapp.
Then I will run the command
php /var/www/listingapp/public/index.php batch/GetPrefecture init > /dev/null &
so I'm expecting that the script will run in background, but after I close the putty CLI and try to re-open it, the process can no longer be found when entering the command
ps aux | grep /var/www/listingapp
So this means it stopped. How do I make it to run in background when session is terminated?
nohup php /var/www/listingapp/public/index.php batch/GetPrefecture init &

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 run a php file forever in ubuntu by command line

I am using web-socket in php to run a chat service problem is when i close the command line the connection also close , is there a solution so that the chat service can run forever .
i am using this command
php -q server.php
create a php file on your server name it as start_server.php
and post this content
<?php
$shell_command = "php -q server.php > /dev/null &";
shell_exec($shell_command);
?>
now execute this command
php start_server.php
I am using screen for running php script and so far I haven't really faced any issues.
screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells). Each virtual terminal provides the functions of a DEC VT100 terminal and, in addition, several control functions from the ISO 6429 (ECMA 48, ANSI X3.64) and ISO 2022 standards (e.g. insert/delete line and support for multiple character sets). There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows moving text regions between windows.
More about screen here.
One drawback with using screen is that if you make a change in your script you will have to terminate the virtual terminal and create a new one for the changes to take effect.
Here is a sample code:
$ screen -L php /path/to/php/script
This will create a new virtual terminal.
Some useful commands:
To detach screen (once you are in the virtual terminal): ctrl + a + d
To resume screen (from your main terminal once detached): $ screen -r
If there are multiple screens running on your machine the above command should be followed by the pid.tty.host string which is unique for each screen
To terminate screen: ctrl + c
To list pid.tty.host strings of each terminal: $ screen -ls

revert 'php artisan serve' command in laravel

I have a laravel app. I ran 'php artisan serve' command for local testing and my app was served at localhost:8000. But now I want to stop serving it at localhost:8000 ie., I want it not to serve now.
I closed the command line, restarted the server but it is still serving. How can it be done?
Note: I am using windows for testing purposes.
here is what i do
press ctrl+ c
> lsof -i :8000
to check if the port is busy or not if any process is listening to the port 8000 it will be displayed with port id
> sudo kill -9 [PID]
kill the process that listen to that port id
run lsof agin
> sudo lsof -i 8000
vola
Press Ctrl + Shift + ESC. Locate the php process running artisan and kill it with right click -> kill process.
Reopen the command-line and start back the server.
Note that you should be able to kill the process just by sending it a kill signal with Ctrl + C.
I used Ctrl + C in my mac for stop the command php artisan laravel serve
For windows user, type this command for showing the list of current running php artisan process:
tasklist /FI "IMAGENAME eq php.exe"
Will show:
Image Name ---- PID ---- Session Name ---- Session ---- Mem Usage
php.exe --------- XXXX ---- Console ---------- 4 ------- 19.852 K
Look up PID number (XXXX), and type this to kill the process:
taskkill /F /PID XXXX
When I use cygwin to run my php artisan serve on Windows machine, pressing ctrl + c does not kill the process. To stop it, use #jsalonen's solution. Basically you are looking for this then kill it:
Windows 10 with XAMPP server depending on what shell you use to run:
php artisan serve
In Windows CMD: CTRL + C
In XAMPP Shell: CTRL + Pause/Break
In Ubuntu and RedHat/CentOS terminal it's: CTRL + C
This work for me.
sudo kill $(sudo lsof -t -i:port_number)
Ctrl + Pause|Break should do it for Windows.
If your using VS Code, simply do the following:
Open VS Code terminal
Find the drop down box that displays a list of all the running shells
Select "php" from the list and click the garbage bin icon
This deletes the shell and kills the server.
In Windows CMD or smder: CTRL + C

Categories