What are the potential pitfalls of using shell_exec() in PHP CLI for running processes in the background?

Using shell_exec() in PHP CLI for running processes in the background can lead to potential security vulnerabilities such as command injection attacks. To mitigate this risk, it is recommended to use functions like proc_open() or popen() which provide more control over the execution of external commands.

$descriptorspec = [
    0 => ['pipe', 'r'],
    1 => ['pipe', 'w'],
    2 => ['pipe', 'w']
];

$process = proc_open('your_command_here > /dev/null 2>&1 &', $descriptorspec, $pipes);

if (is_resource($process)) {
    fclose($pipes[0]);
    fclose($pipes[1]);
    fclose($pipes[2]);
    proc_close($process);
}