Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Consider the following PHP program:

<?echo date("c");
`date`
echo date("c");?>

The backtick-operator `` simply executes the specified command via the shell.

Output on my local windows-box using time instead of date is

2012-02-27T10:21:37+01:00
Current time: 10:21:37[...]
2012-02-27T10:21:37+01:00

As expected, printing the time like this took about 0 seconds.

Output on the production server (Mac OS X) however is

2012-02-27T10:26:29+01:00
Mon Feb 27 10:26:47 CET 2012
2012-02-27T10:26:47+01:00

and when reloading the page:

2012-02-27T10:24:40+01:00
Mon Feb 27 10:24:40 CET 2012
2012-02-27T10:24:40+01:00

So the first time the system tries a system call like this for a new connection it takes 18 seconds and the second and further times 0 seconds.

Here is (almost) the same without running through Apache:

$ time php -r 'echo date("c");echo `date`;echo date("c");'
2012-02-28T10:49:23+01:00
Tue Feb 28 10:49:23 CET 2012
2012-02-28T10:49:23+01:00
real    0m0.890s
user    0m0.011s
sys     0m0.879s
$

Without Apache it takes less than a second even on the first attempt.

I think it is either Apache or some interaction between Apache and PHP.

What settings may cause this and how do I fix or debug it?

share|improve this question

1 Answer 1

I don't know Windows as well as Unix but I believe the following may be related to what is happening on your machine.

On Windows, time is built into command.com and so requires less to be loaded when it is called and, depending on how php etc. works on Windows, may already be loaded and resident in memory. See this page on Wikipedia related to the MS-DOS command.com and resident/transient programs. This area of Windows I am less certain on, I am only offering conjecture based off reading some docs.

On Unix (which includes Mac OS X) when you perform the backtick call from a PHP script (though a similar process happens with any language when you are "shelling out") you are asking the OS to start a shell (probably bash) and then start the date program. This requires the program to be loaded from disk. Then any libraries it needs are also loaded from disk into RAM and linked into the program. Admittedly this should be quite quick; it all depends on your server and what it is up to most the time. Subsequent calls happen almost instantaneously as the kernel will keep the required files cached in RAM from the initial load on the off chance they're needed again in the near future.

I hope this answers your question or at least points you in the right direction.

share|improve this answer
    
Thanks for the explanation. I added some timing of similar code when not running under Apache and there it all runs much faster. –  tomsv Feb 28 '12 at 10:32
    
Well I can only offer a few more hints before you're better off asking PHP folks who know more internals. First, mod_php isn't a separate process, it is running within Apache. Therefore, when you shell out bash will inherit the environment from the httpd process it is running in. This may be larger and causing the shell to be started slower (see here for what bash inherits). Why are you shelling out anyway or is this for academic interest? –  webtoe Feb 28 '12 at 12:37
    
I encountered my problem trying to use the php-function "mail", but while debugging this it turned out that any shell command or fork will take way too much time while running under Apache. I tried to replace the mail-command by shelling out and calling mail/sendmail/postfix from there, but it was no faster. –  tomsv Mar 1 '12 at 14:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.