Fluffy
Fluffy

Reputation: 28362

How to communicate pid from one PHP process to another?

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, but putenv(...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)?

Upvotes: 3

Views: 284

Answers (2)

Carl Zulauf
Carl Zulauf

Reputation: 39558

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.

Upvotes: 1

Blagovest Buyukliev
Blagovest Buyukliev

Reputation: 43508

A common practice is to create a globally-known pid 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.

Upvotes: 3

Related Questions