Php check pid is running

The short answer: You can't.

The medium answer: Outside of the parent process that started your PHP process you can't identify the process with 100% reliability.

The long answer: Bash Process Management

What it boils down to is that top, ps and so on can not be relied upon to definitively identify processes started by another process. Processes die unexpectedly. PIDs are recycled. Process names can be altered.

Can you launch everything from a single, long-lived daemon/process? That's your best bet. At the very least have the parent process store the PID of the PHP process...though this is not foolproof. If you're willing to live with a risk of things going horribly wrong then read that page to find out ways you can mitigate the risks.

(Granted, the tone of that page is pretty rigid but the goal of that site is correctness. If you aren't running a mission critical service or something that people's lives depend on you can fudge things a bit.)

To answer the second part, if you have what you think is the correct PID then you can determine whether it is still running with

kill -0 <PID> && echo 'The process is still running!'

I'm having some trouble on my hosting with too many entry processes running. The hosting company has given me SSH access so I can monitor things more accurately, currently just using top or ps aux. I've found a process that doesn't seem to be finishing off and it's a php script lsphp. However there are a lot of PHP scripts running on the server and I need to figure out which one is the one that is causing the issue. Is there anyway to figure out what the actual PHP script/file that process is connected to is rather than just that the process is being run by lsphp?

For example pwdx 6608 just returns 6608: /opt/cpanel/ea-php56/root/usr/bin which tells me that it's a PHP process but not which actual PHP script started the process.

asked Oct 20, 2017 at 11:44

Php check pid is running

Use

ps -f -p {pid} 

to list the full command line that started a process.

If this is not enough, you can get the list of files open by that process using:

lsof -p {pid}

The output is huge, grep is your friend (keep regular files, exclude PHP runtime, include path with your files, etc....). But the open files may give you a hint.

answered Oct 20, 2017 at 12:24

Php check pid is running

xenoidxenoid

9,4083 gold badges19 silver badges31 bronze badges

2

  • #1

i need to find out which PHP script running but i have only PID. Here is CSF:

Code:

Account: user
Resource: Process Time
Exceeded: 20101 > 2000 (seconds)
Executable: /home/virtfs/user/opt/cpanel/ea-php56/root/usr/bin/php-cgi
Command Line: /opt/cpanel/ea-php56/root/usr/bin/php-cgi
PID: 16659 (Parent PID:15287)
Killed: No

as you can see here is a problem process time which is too long i can kill this process but it start again and that is not solutions

CentOS 7.3 server

Php check pid is running

  • #2

Hello,

The script's file name is not listed, but you should be able to identify the username of the account based on what's listed on the executable line. From there, you could review the files under the account to see which PHP scripts are installed. You may also find the domain access logs for the account helpful when checking to see which PHP files were accessed. They are stored in the following directory:

Code:

/var/log/apache2/domlogs/

Thank you.

  • #3

Tnx for replay. Yes i have account name, but in log files are all php files of wordpress?
i see literally every file for wordpress.
so you say there is no chance to find out which file s running under that pid?

Php check pid is running

  • #4

Hello,

That functionality is exclusive to suPHP with the "full_php_process_display" setting in the /etc/suphp.conf file.

Thank you.

  • #6

@Gojko what was your solution?
thanks

  • #7

as they say it is not possible

How do I know if PID is running?

The easiest way to find out if process is running is run ps aux command and grep process name. If you got output along with process name/pid, your process is running.

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.

How do you check a process ID 1234 whether it is running or not?

In order to check which application is listening on a port, you can use the following command from the command line:.
For Microsoft Windows: netstat -ano | find "1234" | find "LISTEN" tasklist /fi "PID eq 1234".
For Linux: netstat -anpe | grep "1234" | grep "LISTEN".