PHP shell_exec() - Run directly, or perform a cron (bash/php) and include MySQL layer?

Posted by Jimbo on Stack Overflow See other posts from Stack Overflow or by Jimbo
Published on 2012-10-27T21:35:23Z Indexed on 2012/10/27 23:01 UTC
Read the original article Hit count: 134

Filed under:
|
|
|
|

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.

Why?

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! :)

© Stack Overflow or respective owner

Related posts about php

Related posts about linux