What php script is running

Is there any way to know if a php script is running in cli mode?

I have to know on server side if the script called through HTTP request or by command line. what is meant by CLI server?

Is there any way to know if a php script is running in cli mode?

. or the other way around, is there any way to know if a php script is running inside a web server?

Typically, when running in CLI mode, the superglobals $argv and $argc will be set, and many of the typical contents of $_SERVER (e.g. request method) won’t be available. In addition, pre-defined console streams such as STDIN , STDOUT and STDERR will be set up.

You could check the $_SERVER variables . such as:

This will be empty if it is not executed through a web server.

I wrote a small php script that only contained this line:

When I executed it in the commandline, this was part of my output:

rascher@eggs:~$ php test.php Array ( [TERM] => xterm [SHELL] => /bin/bash [SSH_CLIENT] => 192.168.1.104 57547 22 [SSH_TTY] => /dev/pts/1 [USER] => rascher [LS_COLORS] => no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.flac=01;35:*.mp3=01;35:*.mpc=01;35:*.ogg=01;35:*.wav=01;35: [MAIL] => /var/mail/rascher [PATH] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games [PWD] => /home/rascher [LANG] => en_US.UTF-8 [HISTCONTROL] => ignoreboth [SHLVL] => 1 [HOME] => /home/rascher [LOGNAME] => rascher [SSH_CONNECTION] => 192.168.1.104 57547 192.168.1.105 22 [LESSOPEN] => | /usr/bin/lesspipe %s [LESSCLOSE] => /usr/bin/lesspipe %s %s [_] => /usr/bin/php [PHP_SELF] => test.php [SCRIPT_NAME] => test.php [SCRIPT_FILENAME] => test.php [PATH_TRANSLATED] => test.php [DOCUMENT_ROOT] => [REQUEST_TIME] => 1236100063 [argv] => Array ( [0] => test.php ) [argc] => 1 ) 

Running the same thing on the webserver gave me this:

Array ( [HTTP_HOST] => xxxx.com [HTTP_USER_AGENT] => Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.6) Gecko/2009020911 Ubuntu/8.10 (intrepid) Firefox/3.0.6 [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 [HTTP_ACCEPT_LANGUAGE] => en-us,en;q=0.5 [HTTP_ACCEPT_ENCODING] => gzip,deflate [HTTP_ACCEPT_CHARSET] => ISO-8859-1,utf-8;q=0.7,*;q=0.7 [HTTP_KEEP_ALIVE] => 300 [HTTP_CONNECTION] => keep-alive [HTTP_PRAGMA] => no-cache [HTTP_CACHE_CONTROL] => no-cache [PATH] => /sbin:/usr/sbin:/bin:/usr/bin [SERVER_SIGNATURE] => 
Apache/2.2.6 (Fedora) Server at xxxx.com Port 80
[SERVER_SOFTWARE] => Apache/2.2.6 (Fedora) [SERVER_NAME] => xxxx.com [SERVER_ADDR] => 208.109.29.70 [SERVER_PORT] => 80 [REMOTE_ADDR] => 69.134.191.151 [DOCUMENT_ROOT] => /xxxx/httpdocs [SERVER_ADMIN] => jpg@gfhome.com [SCRIPT_FILENAME] => /xxxx/args.php [REMOTE_PORT] => 52187 [GATEWAY_INTERFACE] => CGI/1.1 [SERVER_PROTOCOL] => HTTP/1.1 [REQUEST_METHOD] => GET [QUERY_STRING] => [REQUEST_URI] => /args.php [SCRIPT_NAME] => /args.php [PHP_SELF] => /args.php [REQUEST_TIME] => 1236102678 )

Of note are these vars, which might end up being the most useful:

[argv] => Array ( [0] => test.php ) [argc] => 1 

How to make a PHP file run-able only through CLI mode?, What is the canonical way to determine commandline vs. http execution of a PHP script? Is there any way to know if a php script is running in cli mode? PHP — what is the best & easy way to determine if the current invocation is from CLI or browser; Short story: php_sapi_name().

Читайте также:  Цвет ссылок

What is CLI server?

I am using getallheaders() php global function to get all the headers of the current request. In php manual for it says that it is a alias of the apache_request_headers and it becomes available in CLI server. what is meant by CLI server?

Since version 5.4.0, the PHP command line interpreter (CLI) also works as a built-in webserver.

The command line options needed to start the CLI as web server are:

-S : Run with built-in web server. -****;docroot> Specify document root for built-in web server. 

The function getallheaders() apparently was not implemented in the built-in webserver from the beginning, it was added on version 5.5.7

With the php’s bug #66606, the php’s built -in web server stores the Content-Type and Content-Length header values in HTTP_CONTENT_TYPE and HTTP_CONTENT_LENGTH fields.

You can get these values depending on the value of php_sapi :

if ('cli-server' === PHP_SAPI) < if (array_key_exists('HTTP_CONTENT_LENGTH', $_SERVER)) < $content_length = $_SERVER['HTTP_CONTENT_LENGTH']; >if (array_key_exists('HTTP_CONTENT_TYPE', $_SERVER)) < $contentType = $_SERVER['HTTP_CONTENT_TYPE']; >> 

Why does PHP_SAPI not equal ‘cli’ when called from a, Especially when you want to distinguish the command line interface (CLI) from the common gateway interface (CGI). Note, that the php-cgi binary can be called from the command line, from a shell script or as a Cron Job as well!

PHP vs BASH for CLI scripting?

I have never used PHP with CLI, but I have seen scripts running with PHP code.

  • Why should we use BASH, when PHP is so popular and is able to run in CLI?
  • What are the pros and cons with each one?
  • Should I use PHP for all CLI scripting in the future?

It is still much easier to perform many common tasks in bash. Consider writing PHP equivalents for the following bash one-liners:

# remove a folder full of rubbish rm -rf oldTrashFolder # create an archive of files containing the word 'localhost' tar cf backup.tar $(grep -rlw localhost ~/my-source-code) # make a copy of a mysql database < echo "create database NEWDATABASE; "; mysqldump OLDDATABASE; >| mysql NEWDATABASE 

Bash is more common for this type of application, simply by virtue of it being around forever. But when it comes to choosing one over the other. The language you already know is usually the better choice. Just because you will be more productive.

I guess the main thing is: bash is always there on a recent unix system (its predecessors really are always there, even on ancient unixen). Bash uses every little utility on the system, which can include PHP, Python, Ruby, AWK, Perl (those last two especially).

Consider: how do you get PHP installed? Bunch of bash scripts and a makefile, right? Or your OS package manager, which these days is probably Python, but used to be a bash script as well.

Читайте также:  Html div display border

You can’t administer unix without knowing the shell really well, nor can you write or use makefiles. It may not be the right answer every time, but for a scripting job I always try to figure out if it can be done in bash first.

Higher level languages (like php) have better data structures easily accessible (among other things). However, it’s pretty easy to hack together a working bash script.

How to execute php code at cli?, I want to run simple code at cli and check the value of php setting max_execution_time. Of course I can check it at php.ini or .htaccess. I tried to do it using cli php -a echo ini_get(‘

How to differentiate between http and cli requests?

The title is quiet straightforward. I have to know on server side if the script called through HTTP request or by command line. I could examine the $_SERVER[‘argv’] or $_SERVER[‘argc’] .
What is the pragmatic way to do that?

Look at the keys in $_SERVER. If it is a cli request, you shouldn’t see any that start with «HTTP».

Here is some simple test code:

aj@mmdev0:~/so$ php cli.php |grep HTTP aj@mmdev0:~/so$ 

Possibly checking if no $_SERVER[‘HTTP_HOST’] is set? Because I believe that variable is populated through the Request Headers sent to a file on exection, and the command line probably doesn’t send headers.

You can check if the global variable $argc is set.

Cmd — PHP is not recognized as an internal or external, Follow these steps: Go to System Properties. Go to Advanced. Go to Environmental Variables. Edit the Path environment variable. Add the installation path of xammp/wamp like in my case the path is E:\xammp\php. Click on Ok. Restart command prompt (cmd) Check the version of PHP by running php — …

Источник

How to see what php scripts are running with top or ps aux

Since you’re using php-fpm , I only see a couple of ways to see what it’s doing/done.

php-fpm Status page
You’ll have to configure the php-fpm status page with the pm.status_path directive, as outlined in the docs.

Then you’d need to tell your web server to pass whatever path you used in pm.status_path to php-fpm. That method will depend entirely on the web server.

Once that’s done, you’d access the php-fpm status page and use the full query parameter to show all the workers ( http://servername/status-phpfpm?full , if you setup pm.status_path as status-phpfpm ).

That said, this won’t show you the full history of what scripts have run, just what each worker last ran or is currently running.

Web Server logs
Check the access logs for your web server.

They’ll show you what has run, and if all your PHP is in given directory, or ends with a .php extension, it should be fairly easy to find using grep . Depending on your document root, aliases, URL rewrites and other web server configurations, you might have to do a little bit of interpretation from those logs to find the full file system path of a given script though.

Читайте также:  Linux открыть html файл

This method won’t show you what’s currently running because, as far as I know, most web servers only write to their access logs after the request has finished and an HTTP reply has been sent. This is so that it can log the size and status code.

Источник

Find what PHP script is running on each HTTPD process

I’d like to know a way of inspecting HTTPD processes to find which PHP script is running on them. I already did a «netstat» and found that some processes held DB and Network sockets for too long and now i want to know what scripts are causing it. Btw, i’m using Linux.

2 Answers 2

You need to have Apache module mod_status enabled (CentOs main Apache config file is located at /etc/httpd/conf/httpd.conf)

LoadModule status_module modules/mod_status.so 

with option ExtendedStatus on (this is to be set in the same config file as above)

# ExtendedStatus controls whether Apache will generate "full" status # information (ExtendedStatus On) or just basic information (ExtendedStatus # Off) when the "server-status" handler is called. The default is Off. # ExtendedStatus On 

and some access rights set for that (replace below XXX.XXX.XXX.XXX with your IP — this is to be found in the same config file as above)

# Allow server status reports generated by mod_status, # with the URL of http://servername/server-status # Change the ".example.com" to match your domain to enable. # SetHandler server-status Order deny,allow Deny from all Allow from localhost 127.0.0.1 XXX.XXX.XXX.XXX 

Finally you will be seing what each HTTPD process is doing by accessing http://your-server-name/server-status

This will show the pids and URLs currently being processed in the way presented here.

Someone upvoted this today, so I came back to have a look at what I wrote. Reviewing the question and my answer. yes I answered the question but solving the problem would be better served by logging %D and analyzing the data.

The suggested mod_status is a great alternative to see what process is doing what at the moment, you enable it (and usually add Allow statements to permit you to view it from the IP range you access the webserver from) and then browse to http://site.name/server-status and get nice output. An example can be seen at Apaches own site: http://www.apache.org/server-status

Another useful tool to see whats going on with a process is lsof , if you have a known PID that «hangs» you can type lsof -p to see whats going on with that one. To match all processes you can type something like lsof -c apache or lsof -c httpd . Its a very versatile tool with lots of options for what you want to see.

Finally you have strace that can attach to running processes to see what they are doing at the moment with system calls, etc. strace -p for example. Warning, this CAN sometimes hang the running process, so keep an eye open and restart if needed.

Источник

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