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
- A 46elks account.
- Ability to SSH into a Linux server
- Some experience with PHP
- Some experience with using the command line
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:
||Run once an hour (every hour at minute zero)|
||Run once a day (every day at midnight and minute zero)|
||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:
- Press esc.
- Type “i” (for 'insert') to begin editing the file.
- Paste the cron command into the file.
- Press esc.
- 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
- Sending our reminders and updates to your staff or customers
- Checking the status of your servers and website and notifying your support team
- Requesting monthly feedback from your team
- Checking your database for unpaid bills and sending notices
- Sending yourself weekly SMS updates on your investments
Written 2020-12-18 by Ben Chatwin.