How do i know if php script is running in background?

You can check for the process using ps, but one drawback is that if you have multiple instances of this process running or if the script is hung up then this method can be less than conclusive.

I prefer to actually check if the server is listening on the port. Here are a couple of ways to do this. If your server is listening on port 2000 for example consider the following.

Using lsof

lsof -i :2000; echo $?;

lsof is checking for open file descriptors and should show whether or not a program is listening or actively communicating on this port. This will echo either a 0 if the server is accepting connections on port 2000 or a 1 if it is not.

Using nc

nc -z -w1 192.168.1.12 2000 &> /dev/null; echo $?;

This is my preferred method for checking on a socket server. Here nc is using the -z flag for zero I/O mode to quickly scan the port. You can use your IP address here and the correct port. If the server is accepting connections then life is good.

Again here return values will be either a 0 for good or a 1 for not good. We are discarding any output here because we are wanting just a quick boolean check. This method returns very fast if the network address is reachable. Run from the server itself you will not see hardly any latency as it is trying to talk to itself.

Automating

To run these tests via cron, create a bash script and execute one or both of these commands and run through a series of logical checks. If they fail restart your script and recheck. I have been using these methods for several years now and have had very good results of practical uptime.

I have Windows 10 with xampp installed. Let's assume I have a PHP script, containing

set_time_limit(0);
ignore_user_abort(true);

$counter = 0;

while($counter < 60){
    file_put_contents('runtime_log.txt', $counter." \r\n", FILE_APPEND);
    $counter++;
    sleep(1);
}

I can close the browser window and the script will be still writing output to runtime_log.txt If I wanted to check if it's running in the background on Linux, I would use

ps aux | grep php

How to do the same thing on Windows?

EDIT: I have simplified my question, because I can see that it's been misunderstood.

asked Sep 4, 2017 at 18:26

EnriEnri

255 bronze badges

4

You can't get that information directly from the process list in Windows, because by default Apache is configured to load PHP as a module (a .DLL) so it will not be listed as a different process when it's running (unless PHP is configured to run as CGI, but nowadays that option is not used anymore).

I think that you can search for that using Process explorer, open it and then search (with the binoculars icon) for opened handles for runtime_log.txt, if the file is opened by PHP an open handle should appear for the httpd (Apache) process (this is method I use for finding the program that prevents me from deleting a file).

Note that you probably need to run Process Explorer elevated (as an administrator) since Apache is running as system service.

answered Sep 4, 2017 at 20:17

Alberto MartinezAlberto Martinez

2,5444 gold badges24 silver badges28 bronze badges

Not the answer you're looking for? Browse other questions tagged php windows or ask your own question.

I have created a PHP file that contains an infinite loop that does certain checks and acts if variables change (what's inside the script is probably not very important for my question). I want the service to always run, untill I tell it to stop. After some searching I decided to be using nohup for this task. However, the downside of nohup is that I can't really monitor if the service is down (other than manually running ps -aux and looking for the process).

I've also read things about supervisord, which also sounded pretty good, but I'm kind of lost and don't really know what the best solution is. I want the PHP script to automatically start on reboots and I want the service to check if the script is still running, if not, I want it to reboot/alert so I can check what's up.

Should I be using either nohup or supervisord? Or do you guys recommend something totally different? It would be great if I could run the service inside a docker container where I can simply add the execution of the service inside the Dockerfile.

How do I know if a PHP script is running?

Check if a PHP script is already running If you have long running batch processes with PHP that are run by cron and you want to ensure there's only ever one running copy of the script, you can use the functions getmypid() and posix_kill() to check to see if you already have a copy of the process running.

Can PHP run in the background?

So, to run any background process from PHP, we can simply use either exec or shell_exec function to execute any terminal command and in that command, we can simply add & at the last so that, the process can run in the background.

How do I stop a PHP script from running in the background?

The exit() function in PHP is an inbuilt function which is used to output a message and terminate the current script. The exit() function only terminates the execution of the script.

Where are PHP scripts executed?

PHP code is executed on the server.