What you will accomplish:

Every Saturday morning at 8am an SMS is automatically sent to all gym members with a link to a schedule of the coming week’s class schedule.

What you need to follow this guide

Note: If you are a Websupport customer, you might be interested in this guide instead ... How to schedule SMS with Websupport (Loopia)

Steps

  1. Create a PHP file that sends SMS.
  2. Set up your cronjob

Create a PHP file that sends SMS

This PHP file sends a preset message to a defined list of recipients. In your code editor, create a php file and copy the code so it looks like the example below, except with your 46elks authorization details where indicated by comments


    

Parts of a cron command

It is good to have an overview of cron before we dive in to our example. Below is a brief overview of cron commands and how they work (skip ahead here). Here is a simple cron job example:


    

There are two main components of a cron job:

First, the timing: 10 * * * * - here you set the minutes, hours, days, months, and weekday settings (more below).

Second, the command itself:


    

The command itself in this example has three parts:

/usr/bin/php - PHP scripts are usually are executable by themselves which is why we need to run it through the PHP parser. You can use the command which php to check the correct path.

/www/virtual/username/cron.php - This is just the path to the script.

> /dev/null 2>&1 - (optional) This part is handling the output of the script.

In the above example, the cron job is set to send every 10 minutes - or more specifically, every 10th minute of every hour of every day of every month, every day of the week. An asterisk is a wildcard that stands for 'all'.

Here is a break down of the timing elements:

Here are a few basic examples to get an idea of how it works:

Syntax Explanation
0 * * * * Run once an hour (every hour at minute zero)
0 0 * * * Run once a day (every day at midnight and minute zero)
0 0 1 * * Run once a month (on the first day of every month at midnight and minute zero)

If all else fails, crontab.guru is an excellent resource.

Setting up our cronjob

Below is all the information you need to create a cron job that runs every Saturday morning at 8am. Once you have access to your server, you can check if there are any current jobs running with the following command to list all crontabs. Type the following command into the command line:


    

This will list the crontab contents.
(NOTE: There may be none.)

Now, we'll edit the crontab. Type the following command into the command line:


    

At this point, assuming you've never changed the default editor for you server, you may find yourself in the vim editor. If it is another editor you should be able to follow the directions of that editor. Vim can be a little confusing and scary the first time you use it, so here's what to do inside the Vim editor:

  1. Press esc.
  2. Type “i” (for 'insert') to begin editing the file.
  3. Paste the cron command into the file.
  4. 
        
  5. Press esc.
  6. Type ':wq' (for 'write and quit') to save and exit.

That's it! 🥳 Now you can sit back and your gym members will be happy to receive their updates.

Some other potential use-cases for cron jobs

Written 2020-12-18 by Ben Chatwin.