Check that php is running

How to Check if php is running from cli (command line)

PHP scripts are usually opened in a web browser. But they can be run from command line or terminal as well. The syntax to run a script from commandline is very much similar to python or perl.

Inside a script it might be necessary to test if it is being run from command line or not. For example when a script is being from cron it might produce a different kind of output than when it is run from a browser url. In such cases the script needs to identify its running mode.

There are several ways to check if a php script is running from cli or not. Lets check them one by one

1. Check for the STDIN constant. STDIN is a constant that is defined when php is running from command line.

if(defined('STDIN') ) echo("Running from CLI"); else echo("Not Running from CLI");

However the above method has certain limitations. The test works very well when the script is being run using the php cli binary. In cronjobs for example a the command is specified as follows

Now the php command itself points to a php binary which can be either the php-cli binary or php-cgi binary. If it points to a php-cgi binary, like it happens on some hosting servers then the STDIN check will always be false. So the STDIN check method is not fully reliable. Lets take a look at another method for checking cli.

Читайте также:  Css style link code

2. Check the php_sapi_name. The php_sapi_name function is supposed to return «cli» if the script is running from commandline.

if(php_sapi_name()==="cli") echo("Running from CLI"); else echo("Not Running from CLI");

But this method too suffers the same problem as STDIN. It will work only if the cli mode has been triggered by the php cli binary. If php-cgi was used to initiate the script from the command line, it would always return «cgi-fcgi» for example.

3. Check the PHP_SAPI constant. The PHP_SAPI constant is the same as php_sapi_name function. So it has the same issues as mentioned above.

if (PHP_SAPI === 'cli') echo("Running from CLI"); else echo("Not Running from CLI");

For cgi this approach can be useful :

if(stristr(PHP_SAPI , 'cgi') and getenv('TERM')) echo("Running from CLI"); else echo("Not Running from CLI");

Working Solution

On servers that have fastcgi as the php handler for example, the php binary might point to php-cgi. So to test cli in an interface independant manner a different kind of check has to be done. Which is to check the contents of the $_SERVER variable for example.

function is_cli() < if( empty($_SERVER['REMOTE_ADDR']) and !isset($_SERVER['HTTP_USER_AGENT']) and count($_SERVER['argv']) >0) < return true; >return false; >

The above method should properly detect cli in all cases. Can add an STDIN check too.

function is_cli() < if( defined('STDIN') ) < return true; >if( empty($_SERVER['REMOTE_ADDR']) and !isset($_SERVER['HTTP_USER_AGENT']) and count($_SERVER['argv']) > 0) < return true; >return false; >

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected] .

4 Comments

  1. How ToSeptember 6, 2021 at 3:03 am Not exactly related to the topics, but I am facing a problem related to this. When I run ps -aux in my Namecheap server, it shows PHP processes wither the filenames, but when I run the same px -aux command in y VPS (with LAMP, i do not get the filenames but simply /usr/sbin/apache2 -k start
    Any idea how to make my LAMP server in my VPS to show PHP filenames when I execute ps -aux ?

Источник

How to check if the php script is running on a local server?

Is it possible to check if the website (php) is running locally or on a hosted server? I want to enable some logs if the website is running locally and I don’t want these to appear on the site online.. I can set a variable $local=1; but I’ll have to change that before uploading.. is there anyway to automate this task? Local Server : WampServer 2.0 / Apache WebServer: Apache

7 Answers 7

Check $_SERVER[‘REMOTE_ADDR’]==’127.0.0.1′ . This will only be true if running locally. Be aware that this means local to the server as well. So if you have any scripts running on the server which make requests to your PHP pages, they will satisfy this condition too.

Note that if you’ve done any kind of work on your network 127.0.0.1 may not be your server’s IP address. Be sure to replace the IP above with the actual IP of your server.

also allow for some servers (like Mac OSX) reporting the IPv6 value, so you would need: $local = ($_SERVER[‘REMOTE_ADDR’]==’127.0.0.1′ || ‘::1’) or similar. You can also use the HTTP_HOST value if your local environment uses a different host name like mysite.local, etc.

@Matthieu localhost doesn’t matter since we’re talking IP addresses. Thanks for the comments Charles and ldg!

I believe the best approach is to ‘fake’ a testing mode, which can be done by creating a file in your local environment.
When I used this approach I created an empty text file called testing.txt and then used the following code:

if (file_exists('testing.txt')) < // then we are local or on a test environment >else < // we are in production! >

This approach is 100% compatible with any Operating System and you can use several test files in case you want a more granular approach (e.g. development.txt , testing.txt , staging.txt , or production.txt ) in order to customise your deployment process.

You should automate deployment

This is not directly the answer to your question, but in my opinion the better way. In an automated deployment process, setting a variable like $local = true , like other configuration values (for example your db-connection), would be no manual, error prone, task.

Checking for ‘localness’ is in my opinion the wrong way: you dont want to show your logs to every local visitor (a Proxy may be one), but only when deployed in a testing environment.

A popular tool for automated deployment is Capistrano, there should be PHP-Centric tools too.

If someone is visiting your site via the web, the IP address you see will never be 127.0.0.1 (or ::1 for IPV6), regardless of the usage of a proxy. (Unless of course you’re running the proxy yourself on the same server 😉

Jenkins and CruiseControl with PhpUnderControl are also good ones for continuous integration with PHP.

Источник

How to check if a php script is still running

I have a PHP script that listens on a queue. Theoretically, it’s never supposed to die. Is there something to check if it’s still running? Something like Ruby’s God ( http://god.rubyforge.org/ ) for PHP ? God is language agnostic but it would be nice to have a solution that works on windows as well.

13 Answers 13

I had the same issue — wanting to check if a script is running. So I came up with this and I run it as a cron job. It grabs the running processes as an array and cycles though each line and checks for the file name. Seems to work fine. Replace #user# with your script user.

exec("ps -U #user# -u #user# u", $output, $result); foreach ($output AS $line) if(strpos($line, "test.php")) echo "found"; 

In linux run ps as follows:

You could then do in a php script:

$output = shell_exec('ps -C php -f'); if (strpos($output, "php my_script.php")===false) < shell_exec('php my_script.php >/dev/null 2>&1 &'); > 

The above code lists all php processes running in full, then checks to see if «my_script.php» is in the list of running processes, if not it runs the process and does not wait for the process to terminate to carry on doing what it was doing.

Just append a second command after the script. When/if it stops, the second command is invoked. Eg.:

php daemon.php 2>&1 | mail -s "Daemon stopped" you@example.org 

Edit:

Technically, this invokes the mailer right away, but only completes the command when the php script ends. Doing this captures the output of the php-script and includes in the mail body, which can be useful for debugging what caused the script to halt.

No, during the running of php, the mail command is ran. But, once it stops, it sends an EOF, which causes the mailer to send the mail

#!/bin/bash while [true]; do if ! pidof -x script.php; then php script.php & fi done 

There is so much wrong with this, I can’t believe it. The [true] is invalid syntax and you can get rid of the if by just waiting for the php script to end.

I’ve got a couple of long-running PHP scripts, that have a shell script wrapping it. You can optionally return a value from the script that will be checked in the shell-script to exit, restart immediately, or sleep for a few seconds -and then restart.

Here’s a simple one that just keeps running the PHP script till it’s manually stopped.

#!/bin/bash clear date php -f cli-SCRIPT.php echo "wait a little while . "; sleep 10 exec $0

The «exec $0» restarts the script, without creating a sub-process that will have to unravel later (and take up resources in the meantime). This bash script wraps a mail-sender, so it’s not a problem if it exits and pauses for a moment.

Источник

Оцените статью