Skip to content

Doing php script the silly way

05/05/2011

For the second time in a few months, I noticed a customer doing this on their server (I work at a hosting provider):

curl –silent –compressed http://www.somewebsiteblablabla.com/cron.php

You see stuff like that in crontab sometimes, and it surprises me that someone who knows enough about php to write scripts in it, doesn’t seem to know that it’s a rather inefficient way of executing php code. Here’s what you’re doing:

  • You let the server execute a command within a shell (often through cron).
  • The command opens up a connection to a website through the NIC that is used for internet connectivity, to use to internet to access a website hosted on the same server… again, by using an internet connection.
  • The (local) web server gains an open connection (you, requesting the cron.php page) and handles the request.
  • The web server runs the php code. If there is any output, it sends this back to the shell that executed the request… over the internet.

Doesn’t this sound like going from your kitchen to your livingroom by walking out the door, getting into your car, driving three blocks around your house, parking it next to the front door, ringing the bell and (after your partner lets you in), and walk into the livingroom.

Easier way? Yes there is. If you wish to execute a php script (for example from the crontab), use /usr/bin/php. If it’s not on your server, install php-cli (or a package named something similar to that) or ask your sysadmin to do it for you. How do you run the script? Something like this:

/usr/bin/php -f /var/www/somewebsiteblablabla.com/cron.php

The php command line interface (PHP CLI) has plenty of interesting options, so don’t neglect to read the manual page (man php). This way, it’ll execute the same code, but it won’t use your internet connection and your web server to do it, so it’ll be a bit quicker, and you’ll prevent unnecessary load on your web server. As an added bonus, you don’t need to make the script accessable to everyone through the webserver. You can place the script somewhere on the server where it is not hosted through the web server.

Advertisement
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.