0

I'm trying to run multiple commands through exec without having my PHP script waiting. So far this is what I have but its not working correctly

unzip_file_command ; process_files_command ; delete_unneeded_files  > /dev/null 2>&1  &

Any thoughts as to why this isn't working correctly? I've tried adding nohup to the beginning of the whole command, to the beginning of each, and also adding > /dev/null before each ;. I've tried a bunch of combinations but no luck. Any thoughts? Thanks!

attiliov
  • 41
  • 1
  • 7
  • Possible duplicate of [php exec command (or similar) to not wait for result](https://stackoverflow.com/questions/3819398/php-exec-command-or-similar-to-not-wait-for-result) – Alexandre Elshobokshy Aug 21 '18 at 12:36
  • Possible duplicate of [Is there a way to use shell_exec without waiting for the command to complete?](https://stackoverflow.com/questions/3819398/php-exec-command-or-similar-to-not-wait-for-result) – Alexandre Elshobokshy Aug 21 '18 at 12:38

2 Answers2

0

If you run multiple commands with exec, you need to specify the

 > /dev/null 2>&1 &

for each command separately not just once at the end of the exec. If only placed at the end, it is applied to the last command and you wait the others.

Then you need to place each command inside parenthesis because of the ampersand. So the end result would be:

(commandA > /dev/null 2>&1 &) ; (commandB > /dev/null 2>&1 &)
Corrodian
  • 75
  • 1
  • 12
  • So like this? unzip_file_command > /dev/null 2>&1 & ; process_files_command > /dev/null 2>&1 & ; delete_unneeded_files > /dev/null 2>&1 & because that doesn't work. It doesn't even process the first command. – attiliov Aug 21 '18 at 21:08
  • Oh, sorry. You need to add the commands inside parenthesis if you have the ampersand there. So (unzip_file_command > /dev/null 2>&1 &) ; (process_files_command > /dev/null 2>&1 &) ; (delete_unneeded_files > /dev/null 2>&1 &) – Corrodian Aug 22 '18 at 11:49
0

So I ended up creating a shell script and running that through exec with '> /dev/null 2>&1 &' at the end. It seems to have solved the problem.

attiliov
  • 41
  • 1
  • 7