Mitchell Bennett
Mitchell Bennett

Reputation: 27

PHP exec() not storing Process ID

I have a PHP program which is starting a process in the background via exec() (the output of the process is being sent to /dev/null). I then use exec("echo $!", $processes[0]) because I want to store the ID of the process so I can terminate it later. However, the result of $processes[0] is an empty array.

Why is the process ID not being stored? Note that the process ID is outputted correctly if I execute my background process and then run echo $! from the terminal. An additional note is that I am using a Raspberry Pi which is Linux-based.

Upvotes: 0

Views: 1806

Answers (1)

Photozrme
Photozrme

Reputation: 71

In order to record the process ID, you have to set it into a variable. For example, to start another PHP script in the background:

$pid = exec("php mysbackgroundscript.php >/dev/null 2>&1 & echo $!");

The $pid variable should then contain the PID.

Upvotes: 3

Related Questions