0

I'm trying to run my server from PHP script as a background process, but it's hanging the PHP script anyway. I call it like this:

$exec_result = exec('./myapp option1 option2 &> /dev/null &');

I tried things from PHP hanging while exec() bash script like adding "set -m && " or "shopt -u checkjobs && " but that doesn't help. I also tried to call in exec() my C++ utility that runs command in background (basically just calls std::system with " &"), but that didn't help either. Using "nohup" doesn't change anything. Also, the problem is not in my server because same thing happens when I call "sleep" command.

Calling exactly the same command from bash runs process in background as expected. Honestly I'm so confused and frustrated. What am I doing wrong? Maybe PHP needs some kind of permissions to run a background task? I'm kinda new to Linux.

I'm doing it all from Debian 10 and PHP 7.3 if it matters.

oguz ismail
  • 1
  • 16
  • 47
  • 69
Victor
  • 337
  • 2
  • 8

1 Answers1

0

I've managed to fix it, but I have no idea why the new solution works while the old one doesn't. Maybe it has something to do with exec() build-it parser? Both lines work identically in bash so I'm blaming PHP on this.

So, I've replaced

$exec_result = exec('./myapp option1 option2 &> /dev/null &');

with

$exec_result = exec('./myapp option1 option2 > /dev/null 2>&1 &');

and that did it. I've checked it back and forth multiple times and the second line works consistently while the first one fails every time.

Victor
  • 337
  • 2
  • 8