3

Let's say I want to execute the command mycommand with PHP shell_exec() 10 times. Should I do a bash loop:

shell_exec('for i in {1 .. 10} do mycommand -i done');

or rather a PHP loop:

for($i = 1; $i <=10; ++$i) { shell_exec('mycommand -'.$i); }

What are the reasons (security, performance, style, ...) to choose one over the other?

Foo Bar
  • 1,764
  • 4
  • 24
  • 43
  • 2
    To me it sounds better to use the first option. After all, you will be opening a shell just once, instead of ten times. – fedorqui May 21 '14 at 08:38

2 Answers2

4

Go for bash loop, because shell_exec function is called only once. It will be faster than calling shell_exec multiple times. Enabling functions like exec, shell_exec itself a huge security issue. If someone managed to upload a PHP shell in your server then he can hack your server.

Harikrishnan
  • 9,688
  • 11
  • 84
  • 127
0

first option execute shell script once and loop in that. which is faster. whereas in second in each loop shell script is calling multiple times which is slower than the first.

Satish Sharma
  • 9,547
  • 6
  • 29
  • 51