5

Can anyone tell me why exec() function is so slow and time unpredictable on different machines in PHP?

Basically I have some executable file and want to execute it through PHP:

$command = '/usr/pathToComman/myCommand -someParameters';
exec($command);

The thing is that it executes much longer (by much I mean 3-4 sometimes like 25 times longer) then the same command from the shell.

In additional to this the command is executed longer from the PHP on my server, which is stronger (more RAM and more GHz).

So there are two questions:

  • why?
  • what should I do with this?

P.S. I need this execute, because I can not do the same thing with PHP

P.S.2 Answering @prodigitalson question: It solves the differential equation, but basically no matter what it does, the speed is just much slower. I remember doing some image processing this way and the speed was also much slower.

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • 1
    see this thread: http://stackoverflow.com/questions/1757160/calling-bash-with-phps-shell-exec-slow – Dawson Loudon Nov 17 '12 at 01:08
  • So on the same machine it takes longer started from PHP than from shell? (Maybe include some numbers in the question.) Is there any load on the box? Maybe it inherited a nice value from the web server process. – AndreKR Nov 17 '12 at 01:26
  • I belive i have figured this out see https://stackoverflow.com/a/48505455/6342916 – Josef Fort Jan 29 '18 at 16:30

1 Answers1

1

The problem why this is slower is probably because your PHP server has to go to the shell to run your command. So what the PHP server does then is create a new shell and call the command on the new shell. Creating a new shell means in most systems to create a new thread. So all those things together result in a longer execution time.

Sometimes a cgi script can help with those problems, but I'm not sure if it will help here (because I don't really know what you are trying to do on the shell)

ndsmyter
  • 6,535
  • 3
  • 22
  • 37