How to automate processes using cron jobs

In previous tutorials, I’ve covered common actions that most of us will encounter on a daily, weekly or monthly basis as we manage our respective church operations using Church Community Builder (CCB).

Although CCB is great tool and a leading web-based Church Management Software, the current version does not contain *true* automation that automatically processes individuals based on a set of performed behaviors or simply time-based automation actions.

Yes, CCB does allow for you to create processes and queues. However, to process individuals from process to process and queue to queue, a person has to manually process and queue individuals. And what a time and labor intensive process this is and can be for numerous church administrators.

Not having true automation features for process and queues leads to a slightly unmanageable bottleneck of always depending on persons to remember to check and process queues for processes to be truly beneficial and worth their weight in gold. This can be a problem, a huge one at that, due to us humans always increasing and introducing the opportunity for error when managing a manual process.

Now, this does not mean that CCB’s development team will not include true automation functionality in the coming versions. I’m not privy to CCB’s product roadmap for future releases and features, but I would imagine that true automation throughout their product is on their roadmap and slated in the near future.

That being said, this automation talk leads me to the point of today’s tutorial in showing you how to automate repetitive tasks and operations using cron jobs. Simply put, cron jobs are nothing more than timed automated steps that consistently perform business logic.

In today’s tutorial, I’ll show you how to automate the process to backup your CCB data, and the birthday emailer using cron jobs in my Bluehost account. Not only will you save time and multiply your church’s operational efficiency, you’re going to fall head over hills in love with the consistency that using cron jobs can and will bring to your church operations.

What is a cron job and why use one?

Think for a minute of all the various tasks you perform repeatedly on a daily or weekly basis. You perform the same task day in and day out, week in and week out. Not one thing changes about these tasks you perform.

Maybe the tasks are in relation to a weekly report you email to staff. Maybe it’s a weekly attendance or church metrics report. Maybe you would like to perform system maintenance like deleting files you no longer need based on their age, type or size. Or maybe you’re in need of backing up your website or a database on regular basis without having to set calendar reminders to manually perform such actions.

If you manually perform tasks that don’t change and they depend on systems for input or output, then let me introduce you to the cron job. A cron job is nothing more than an automated time-based job scheduler that can be set to run by the minute, hour, day of the week, day of the month, month or any combination of these.

Simply put, cron jobs consist of time elements (i.e., minute, hour, day, month, etc.), a script to be called or executed, a command that executes the script (e.g. cURL, php, perl, GET, WGET, etc.), and the output or actions performed by the script. Read more on cron jobs here…

Typically found as a software utility in Linux and Unix, Windows has a comparable service called Task Scheduler as does Mac with its Automator. But enough about the origin, let’s set one up…

Preparing to send an automated email

I’m not going to go into great detail about setting up a cron job although I cover setting up an example php emailer in the video below.

As a test, setup a cron job to execute your CCB data backup file, birthday emailer or create a simple php emailer soon to be discussed.

To get started, open your text editor, and create and save a file named cron-job-emailer.php. And before I forget, be sure that you have PHP installed on your local or web server (most do by default). This is important due to the fact that we will use a built-in PHP mail function to execute an email send.

Now that you have your file, create the following variables: to, from, from_name, subject and message. You’ll see in the example codebase below that I’ve assigned each their own variable string values. Feel free to replace and change the variables as necessary.

You’ll want to be sure to set the to variable equal to your email address for sure. In addition, you may want to set the from variable to be an email address for your church (e.g. info@yourchurchdomain.com).

The next set of concatenated variables is the headers variable. I’m not going into great detail about the headers, but I do encourage you to read up on the mail() function for intimate knowledge of header sections.

Just know that this portion of code is a very important piece of the emailer. In short, you’re setting the email type and encoding so that the message can be sent by you and received by the user.

Once the headers variables have been set, you are now ready to create a sendEmail variable and assign it the mail function, passing to it the to, subject, message and headers variables as arguments.

Finally, add if else logic at the end of this process to know whether or not the email message was sent using the mail function. Once you do this, you are now ready to place cron-job-emailer.php into your web directory, and now it’s time to setup the cron job to execute the email script.

You can also test the file by opening it in a web browser or execute php cron-job-emailer.php in the command line of your local or web server terminal hosting your file just to be sure that it executes and sends you an email.   Just an inside testing trick.

Set up the cron job, and let’er rip and reap the benefits of increased operational efficiency!

Now of course, you can use the cron-job-emailer.php file, or you can choose to use the CCB backup or birthday emailer files to execute when setting up your cron job.

In this example and as shown in the video tutorial, I use the php command followed by the internal path to the cron-job-emailer.php file on the web server. In the video, I set it to run the job on a daily basis by using the out-of-box common settings offered by my Bluehost Linux account.

Once setup, hour on the hour, I’ve been receiving the same test email without a fail.  In addition, the cron job also sends me a system email to alert me that the cron-job-emailer.php file had been successfully executed.

In closing, typical web host service providers that offer cron job services as a part of their hosting packages provide an intuitive user interface to manage cron jobs. In the case a graphical user interface (GUI) to manage cron jobs is not provided, then you’ll have to manage cron jobs using the command line interface.

In the mean time, take a quick crash course review of the video tutorial below, and I’m sure you’ll be automating a multitude of operational processes to no end. And if there is an end, it’ll be one that is of increased efficiency and less manual labor. 😉

Well, happy automation using cron jobs… that’s it!  Until the next post, cheers and let me know if you have any questions!


Please comment if this tutorial has helped you.