Plannedscape Postings

Image

Cron Jobs
How To Automate Almost Anything On Your Website

Posted by Charlie Recksieck on 2022-05-05
I want to take a moment to talk to you about automating actions on your website. There's a term and type of file that handles these and they're called "cron jobs".


CRON Job Definition

"Cron jobs are usually used to schedule commands at a specific time. You can use them for tasks like running backups, monitoring the status of the system, or running system maintenance tasks. Cron jobs are a helpful utility for system administrators." - from freecodecamp.com.

More technically, cron jobs are a command-line job-scheduler on Unix and Linux systems to run/call routines periodically at fixed times or intervals.

FYI, "cron" is not an acronym, its name is taken from "Chronos" the Greek personification of time.


Let’s Be Clear, Cron Is Not Code

We want to be clear that the cron file does nothing other than open/run files at specfiic times. It’s not code. The lines are just commands to open files. Typically on a server you might call a shell script, a database stored procedure, or open a web file (like PHP or ASP).


What They Look Like



As you can see in the above screen capture, you can schedule events to happen every minute, hour, day, month, year or on any specific time.

Also, your time zone needs to be defined. You do this in a crontab environmental variable - learn how here


Typical Uses

- Backups - log files or databases
- Auto delete old backups
- Notification emails
- System maintenance
- Auto deploy staged changes at an acceptable time.

Keep in mind that cron jobs don't actually involve much code. They just run on the server and if the particular time is supposed to execute an action, then the cron job calls a script or a URL. That code or page itself is what executes the backup, compiles and sends notification emails, etc.


A Couple of Our Examples

For our frequent flyer points management app, Points Pilot, a daily cron job is the key to the whole operation. Once a day, the cron job calls an ASP page which looks into databases and complex code to generate a lot of reminder emails to users.

On a tennis clinic application, we have a nightly cron job call a page of php code that creates new instances of clinics in the database - which means that as soon as visitors go to the website, a new blank clinic is visible on the site dynamically.


Time-Triggered Things You Use Right Now

We take these for granted but there are plenty of features we use in software that are time-triggered actions.

- Outlook calendar reminders
- Hootsuite social media apps
- Smart home apps


If This, Then That

In case you don't know about it, there is an absolutely incredible service called If This Then That (www.ifttt.com) that has thousands of easy and ready-to-use applets to connect to Spotify, your phone, Alexa, and so many more services - they can act as the interfaces between anything.

If you can dream up any scenario you want to automate, IFTTT can probably pull it off for you.


Wanna Learn More?

Here’s some good resources, especially depending on your site host type:

- Tutorial and command creator: Crontab.guru
- WordPress: Wordpress doc
- cPanel: cPanel doc
- AWS: "Lambda" Amazon documentation
- Azure "Timer trigger" Microsoft documentation


Or ...

Feel free to reach out to us for a quick lesson on setting up a Cron job in your environment. We'd love to hear what your needs are ... and we'd love to get to know your business, and you get to know ours.