wget in combination with cron is a useful utility for automating processes in a web application. It’s possible to run most scripting languages from the command line, but sometimes it’s tricky to get those to run correctly when they don’t have the server context.

One simple way to achieve this is to write your automation functions as a regular web-based page within the app and then use wget to post a web request to that URL. You can use cron to schedule the frequency.

wget is fairly simple to use, however one thing that is difficult to find in the documentation is how to get the contents of the document and append them to a file. The normal behavior is to just download the file and save it with it’s original name. You can remedy this by specifying a dash “-” as the output filename, which tells wget to output the document to the console. the –quiet parameter tells wget not to output a bunch of connection info. Finally, we redirect the console output to append to a file using >> at the end. The resulting statement looks something like this:

wget --quiet --output-document=- http://localhost/auto.php?arg=123 >> /path/to/file.log

Depending on your server configuration you may need to put quotes around your URL as well, like so:

wget --quiet --output-document=- "http://localhost/auto.php?arg=123" >> /path/to/file.log

If you are attempting to connect to a secure URL (beginning with “https”), you may also need to add the “–no-check-certificate” option to the command in which case your command would like like so:

wget --quiet --no-check-certificate --output-document=- "https://localhost/auto.php?arg=123" >> /path/to/file.log

If you have your automation script output debug information, then your log file will contain useful information. As an example, I like to output the date and time that the script runs and then any info about failures that may have occurred.

One note about security – if you don’t want people poking around and firing off your automation and/or viewing your debug information, you should implement some type of security, such as a simple username/password arguments or something more robust if required.