Take the 2-minute tour ×

Sorry if the title is vague - I wasn't quite sure how to word it!

What I'm Doing

I'm running a Linux command to output data into a variable, parse the data, and output it as an array. Array values will be displayed on a page using PHP, and this PHP page output is requested via AJAX every 10 seconds so, in effect, the data will be retrieved and displayed/updated every 10 seconds. There could be as many as 10,000 characters being parsed on every request, although this is usually much lower.

Alternative Idea

I want to know if there is a better* alternative method of retrieving this data every 10 seconds, as multiple users (<10) will be having this command executed automatically for them.

A cronjob running on the server could execute either bash or php (which is faster?) to grab the data and store it in a MySQL database. Then, any AJAX calls to the PHP output would return values in the MySQL database rather than making a direct call to execute server code every 10 seconds.


I know there are security concerns with running execs directly from PHP, and (I hope this isn't micro-optimisation) I'm worried about CPU usage on the server. The server is running a sempron processor. Yes, they do still exist.

Having this only execute when the user is on the page (idea #1) means that the server isn't running code that doesn't need to be run. However, is this slow and insecure?

Just in case the type of linux command may be of assistance in determining it's efficiency:

shell_exec("transmission-remote $host:$port --auth $username:$password -l");

I'm hoping that there are differences in efficiency and level of security with the two methods I have outlined above, and that this isn't just micro-micro-optimisation. If there are alternative methods that are better*, I'd love to learn about these! :)

share|improve this question
*DEFINE: better { More appropriate, advantageous, or well advised | there couldn't be a better time to start this job | it might be better to borrow the money } –  Jimbo Oct 27 '12 at 21:36
Does this data change often? –  slashingweapon Oct 27 '12 at 22:21
Yes it changes every second. But I only require data for the last 10 seconds. –  Jimbo Oct 27 '12 at 22:24
to grab the data and store it in a MySQL database - where is the data coming from? How immediately after the data is available does it have to be written to the database? How immediately does the information have to be available to users? –  symcbean Oct 27 '12 at 22:25
How long does it take to retrieve the data? I'm starting to think you should create the data when it is needed. –  slashingweapon Oct 27 '12 at 22:27
show 2 more comments

2 Answers

up vote 3 down vote accepted

I think the idea of a separate cron job doing the data processing is a sound one. You're right -- directly executing server commands from a PHP script is not the most secure way of doing things and generally should be avoided.

However, from a performance standpoint, you may not want to be accessing a MySQL database constantly for the data either, especially if it's always changing and updating. I would suggest using a key-val storage system like Memcache, or if you don't have much memory available, something that's a hybrid of disk/memory storage. Your background process would update the value of the key at regular intervals and then you'd access that from your main web server process.

share|improve this answer
Just learned how to use this. Elegant solution, thanks! –  Jimbo Oct 28 '12 at 17:10
add comment

If you are going to produce all the information you need for all of your users every 10 seconds, then maybe you should:

  • Use PHP for your cron job
  • Generate the data you want in the format in which it is to be served
  • Save the output files in your web folder heirarchy
  • Let the client retrieve it as a static file
share|improve this answer
Wouldn't have thought of using static text files. Repeatedly writing to and reading from these - would this be more efficient than the memcached option? –  Jimbo Oct 27 '12 at 23:17
Yes, it would be more efficient than memcached. –  slashingweapon Oct 28 '12 at 22:59
One caveat: If memcached is running on the same machine as the web server, then memcached might be faster than file access. But if you're going to go that route, you might consider APC instead of memcached for your caching. –  slashingweapon Oct 29 '12 at 20:52
add comment

Your Answer


