linux - How to communicate pid from one PHP process to another?

802

There is a long-lived PHP process, which supports signals (SIGHUP etc.) and another PHP process needs to know the PID of the first one to send a signal.

I'd like to set an environment variable, which would be read in the second (client) process and used for my purposes, butputenv(...posix_getpid()...) doesn't seem to make it visible to another process.

How do I make an environment variable, set in one PHP process, visible to another? If that's impossible, what's the preferred way (I'd like not to use files or db for this)?

780

Answer

Solution:

A common practice is to create a globally-knownpid file when starting the daemon process, and writing its PID to that file.

Other processes willing to send signals to the daemon just read the pid file to retrieve its current pid and send the signal thereafter.

513

Answer

Solution:

What you are talking about is Inter-Process Communication (IPC). Solutions include using a socket, a named pipe, a file, redis, some message bus/queue, etc. This is a common and complex problem in software development, and one with many many robust options.

Sounds like a simple file would work fine for you and is probably the... simplest.

People are also looking for solutions to the problem: URL for the pdf file using PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.