Automate Your Life With Cron

Example 1: Cleaning Up Files Scenario

We provide a service rendering 3D models of circuit boards based on individual electrical layers exported from CAD software. Users upload the layers for their circuit board projects via a web interface, select render quality options, and press a Start button to begin the process. Once the final 3D rendering is processed, the rendered product is sent to the user. If the user is unhappy with the results, he or she may change the render quality options and try again. We want to keep the user's uploaded layer files on the system in case he or she wants to render again with different quality options. However, we do not want to keep them forever as we would eventually run out of disk space.

Solution

We can solve this problem by periodically deleting old files that are no longer needed. It wouldn't be a difficult task to do manually, but it would certainly be a waste of time. We can create a cron job and use built-in Linux commands to do the work for us!

/etc/crontab
@daily  ben  find /upload/layers/ -type f -mmin +120 -exec rm {} \;
Explanation

We know from the Using Cron page that the first element is telling Cron to run this job every day at midnight and the second element is the user who will be running the cron job. The command itself is a bit more involved, however. Let's break it into smaller pieces and see what it's doing.

Piece Explanation
find /upload/layers/ Run the Linux find command to search for files in the /upload/layers/ directory
-type f Only return files that are of the type "regular file"
-mmin +120 Only return files that were last modified over 120 minutes ago
-exec rm {} \; Execute the rm (remove) command on each file found.
  • The string '{}' is replaced by the current file name being processed
  • The semi-colon ';' is used to mark the end of arguments to the command and is escaped with a slash '\' to protect from expansion by the shell

So, every night at midnight, our Cron job is deleting any files that have not been modified in over two hours. You may be wondering, why not just delete all the files every night at midnight? Consider the following: if a user has submitted his or her layer files at 11:50 p.m., he or she is likely still using the files. We want to make sure we only delete files that have been sitting dormant for a while (two hours in our case). This will keep our disk usage to a minimum and we won't have to worry about manually deleting files ourselves. Thanks Cron!

Let's move on to a more complex example now.



;