How to Prevent a Cron from Creating Empty Files in Root

This simple guide will show you how to prevent a cron job from filling the root directory of your hosting account with empty files. If you have this issue, your cron probably looks like this right now:

wget -q http://www.a-different-domain.com/somescript.php

When wget is called, it visits the location and retrieves/writes a results file from the web page it is called to. Even if the page that is being called doesn’t have any output, the wget function will still create an empty file. Since there was nothing to retrieve, the result is empty.

There is a simple fix that will prevent the blank file from being created. By simply adding -O option to your wget command, you can specify a location to write the result to on your server. When you specify wget to write the file to the location /dev/null, the file will actually be discarded instead of written to a file. Your new cron job command should look similar to this:

wget -q -O /dev/null http://www.a-different-domain.com/somescript.php

The reason this works is that you are telling wget to visit your webpage to get results, -q to quiet the response, and -O to write the results files to the location /dev/null. Since /dev/null is a null device, the result is simply discarded.

That’s it! By adding this one simple option, you won’t have blank files constantly filling up your root! Goodbye pesky empty files!

Bonus tip! If you have a cronjob such as the following

* * * * * /bin/sh /something.sh

You can add “> /dev/null 2>&1” to the end of it to produce the same effect. Here is an example:

* * * * * /bin/sh /something.sh > /dev/null 2>&1

5 Essentials for Keeping Your Website Safe


Disclaimer: WebCitz, LLC does not warrant or make any representations concerning the accuracy, likely results, or reliability of the information found on this page or on any web sites linked to from this page. This blog article was written by David W in his or her personal capacity. The opinion(s) expressed in this article are the author's own and may not reflect the opinion(s) of WebCitz, LLC.