Best Cron Alternative You're Not Using

Divine Orji - Nov 21 '22 - - Dev Community

Cron is a time-based scheduler for Unix-like operating systems. Developers use cron jobs to automate database maintenance activities, perform general chores, and set up schedules for things like notifications or reminders.

As a developer, if you’re using a popular content management system like WordPress or Drupal, chances are good that it comes with built-in support for cron jobs. However, if you’re building from scratch, you may notice issues when running cron jobs on your server.

In this article, you will see some of these issues and learn about a suitable alternative to Cron: Appwrite.

How A Cron Job Works

On a Linux or macOS (*nix) server, the cron command line interface runs instructions from a crontab file:

Cron syntax

These instructions follow a specific format:

  • Five fields represent the minute, hour, day of month, month, and day of week for your task to run. There’s an optional sixth field for year, but developers rarely use it
  • By default, each field contains the * symbol, also known as a “wildcard”. If, for example, it is on the “month” field, it will run your set command every month of the year
  • Your server’s username
  • The command you want to execute
  • The time settings, username, and command are all separated by spaces

So if you want to write a command to add some text to a file at 2:45 am every Tuesday, you will first run this command in your server’s terminal to open your crontab file:



crontab -e


Enter fullscreen mode Exit fullscreen mode

Then add this text to the file:



45 2 * * 2 username echo "some new text" >> path-to-file/file-name


Enter fullscreen mode Exit fullscreen mode

The username is usually root, but you can specify a different username as required.

Why You Shouldn’t Use Cron

Cron was created in the 1970s and has since evolved, but it remains a niche tool that is not always appropriate for every use case.
For example, it is possible to mismatch timezones when setting up a cron job, causing your command to run at a different time than expected.
Some other reasons why you do not need to use cron include:

  • It has no auto-retry mechanism, so if your server stops or restarts, your cron job will not run until its next interval
  • You have to specify a new line at the end of your crontab file, or your command will not run
  • In many environments, cron executes commands with sh instead of bash.
  • You cannot schedule a task to the exact second with a cron job. The shortest possible time in a cron syntax is one minute

Appwrite: An Alternative To Cron

Appwrite - Open-source backend-as-a-service

Appwrite is an open-source backend-as-a-service platform that offers a set of REST APIs to help you meet your basic backend needs. You can add features like authentication, cloud services, databases, file storage, and webhooks to your application without starting from scratch. You can also extend Appwrite’s capabilities using your favorite backend language.

One of Appwrite’s services—Cloud Functions—executes your custom code in response to system events like creating an account, logging in, or document update.
It also allows you to schedule serverless functions using a cron syntax or manually trigger them via an HTTP endpoint.
Appwrite runs your functions in a secure, isolated Docker container and supports multiple runtimes for different backend languages.

Scheduling your tasks using Appwrite Functions has some unique advantages over traditional cron, such as:

  • You can write backend functions and deploy them without fully managing any infrastructure. Appwrite’s reliable cloud services ensure that your servers perform optimally 99.9% of the time
  • Appwrite manages scheduling for you on its platform, which means that in the rare event your task misses its schedule, it automatically retries it
  • Instead of dealing with inconsistent timezones, wrong crontab notations, or manually restarting your cron after a server update, you can easily set up your task schedules in Appwrite’s delightful user interface and achieve the same results or better

Scheduling Tasks With Appwrite Functions

To schedule a task, create a serverless function in your preferred backend language and deploy it to Appwrite.
On the sidebar of your Appwrite dashboard, click on “Functions”, select the function you have created, and go to its settings.
When you scroll down, you will see “Schedule (CRON Syntax)”, which enables you to set a schedule for your function or task:

Scheduling with Appwrite

You can set the minute, hour, day of month, month, and day of week based on the original cron syntax.
After setting up your schedule, click the “Update” button below, then sit back and watch it execute reliably with no extra supervision.

Conclusion

In this article, you learned about task scheduling using cron and how to set it up on Appwrite Functions. You also discovered some benefits of using Appwrite Functions instead of traditional cron.
Take advantage of Appwrite’s full range of backend services, including database, file storage, and user authentication, to build and scale your application.
Click here to get started.


Cover image by pch.vector on Freepik

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .