Cron is a job scheduling utility, typically for Unix-like operating systems. Cron jobs are Linux commands that run periodically in the background based on predefined configurations or criteria. These criteria might include a specified time interval, event occurrence, or set condition.
For example, you can use cron jobs to take a weekly database backup, generate a daily report of failing records, or clear log files every two days at 8 a.m.
While cron is one of the most popular methods of job scheduling, monitoring cron jobs to ensure they work as intended can be challenging and time-consuming. For example, one approach to monitoring cron jobs requires appending the script’s output to log files and reviewing the responses to determine task execution. Another approach to monitoring jobs is to manually check their status, which is not feasible at scale.
Monitoring cron can be time-consuming and open you up to human error. Additionally, jobs can fail silently, and without looking at the log files or checking the status of your jobs, it's difficult to know when this happens. This can lead to security risks and system failure when critical jobs fail.
In this article, we walk through how to monitor cron jobs, some of the challenges you may face when doing so, and introduce a maintenance-free alternative to cron: Airplane. Airplane is a developer platform for rapidly building internal UIs and workflows. Airplane schedules provide a reliable tool for creating, managing, and maintaining jobs. We walk through more of Airplane's functionality towards the end of this article. If you're looking for a solution to help create and maintain scheduled jobs safely, you can get started with Airplane for free.
Monitor cron jobs
Let's start with a quick introduction to the components of cron jobs and how to monitor them. Typically, a cron job consists of three major components:
- A script that you intend to execute
- A schedule of when to execute the script
- Optionally, the result or output of the execution
There are a couple of approaches for monitoring cron job execution:
- A somewhat manual approach is by piping the output to a log file and checking the file content to confirm the task execution.
- Another approach is verifying the status of the cron job itself. For instance, if you run a daily backup job, you can confirm the job’s execution status by writing the script response to a log file. The output tells us the status of the cron job, success or failure, and the reason behind the failure.
In the next section, we cover the syntax required to create and schedule cron jobs to run at specific intervals.
Cron jobs are written in a crontab file, which is a simple text file containing cron commands to run at specific times. Usually, the default location of this text file depends on the user's operating system. For most Linux flavors, the file is in the
/var/spool/cron/crontabs directory. The individual cron commands in this file have five fields specifying the exact date, time, and interval to execute the script and the corresponding command in this order:
* * * * * <command to be executed>
From the right:
- The first asterisk (*) represents the day of the week (0-6 with 0 being Sunday)
- The second represents the month (1 - 12)
- The third represents the day of the month (1 - 31)
- The fourth represents the hour (0 - 23)
- The fifth represents the minute (0 - 59)
Built-in methods for monitoring cron jobs
Now that we discussed basic cron syntax, let's walk through how to use the cron's built-in method to verify its status to ensure the cron itself is running and review the System Logging Protocol (Syslog) to see if it's executing tasks.
Let's start off with a simple example — assume you want to clear log files from the
/Users/someuser/temp/* directory every day at 8 a.m.
First, create a new cron job using the
crontab -e command. The
-e allows you to change a crontab file. If no file exists, it allows you to create a new one.
Note that based on the operating system, it might prompt you to choose an editor. For macOS, the option doesn't exist. However, you can use an environment variable or edit the
bash_profile file. To use nano, you can run this command:
env Editor=nano crontab -e.
Once the crontab is open, add this line to delete the contents in the temp directory:
* * * * * rm /Users/someuser/Downloads/temp/*
Run this command to check the available running cron jobs:
Note that cron reads and understands the crontab’s syntax and executes the commands in the script in an automated way. In the above command, the
-l flag means “list” and allows you to display the contents of the crontab file.
Cron jobs don’t have any native notification features. One straightforward and typical way of monitoring cron jobs is to manually write the output to log files. Then, you can review these log files to determine if a cron job running and executing functions successfully. In the next example, we demonstrate how to verify cron task execution by reviewing a log file.
Determine cron job execution
Next, let's determine if our cron jobs are actually executing. To do so, we can write another cron job to show a log file with both error and success cases every minute.
To begin, let's create a new
demo.sh file inside a demo directory. You can use the following command to create a demo directory:
Navigate to the
demo directory with the command
demo.sh file using the touch command
Add the following inside the
As you can see,
success.log files. The
demo.sh file appends error responses to the
error.log file and success cases to the
After this, we'll grant access permissions to the
demo.sh file so that it can be executed.
Run the command
chmod +x demo.sh.
Next, write the commands in the
Open the crontab editor with nano with the command:
env EDITOR=nano crontab -e
Next, add the cron execution script:
* * * * * /Users/someotheruser/demo/demo.sh
This script runs the
demo.sh command every minute. To verify, check the
As you can see,
demo.sh created the
To see the output of the
success.log file, use the
cat error.log and check the output of the
error.log file. Since there is no file named
demo_error/demo.txt, you see an error message.
This is one common way to monitor your cron jobs to ensure they're performing as expected. This approach becomes increasingly risky at scale which is why you may want to consider substituting cron with another tool.
Maintenance-free job scheduling with Airplane
Airplane is a developer platform for quickly building internal tools. Engineers can transform scripts, queries, and APIs into UIs and workflows that can be shared safely. Airplane schedules take care of the majority of challenges that developers face when using things like cron and other open-source job schedulers. Airplane has out-of-the-box support for features like permissions, RBAC, and audit logs that make it an easier-to-use and safer alternative to cron.
Airplane has a number of advantages over cron
Airplane automatically logs all jobs. The Activity page shows a log of all events with event details, actors, and timestamps. Airplane also allows you to easily filter and export your audit logs.
On the Activity page, you'll also see the task's status to determine if tasks have succeeded or failed.
Airplane allows you to automatically receive notifications via Slack, email, or the notification inbox for increased security and easier system maintenance. This also makes it much easier to troubleshoot issues with your tasks.
You'll never miss a job failure again with Airplane's notification subscriptions. You can customize your notification settings and also subscribe to specific events on your jobs.
Security and access controls
Using Airplane, you can set granular permissions for who can view, request, execute, and be an admin on a task-by-task basis. You'll be able to create group-based and role-based access controls which are not supported with crontabs.
Central control and easy-to-use UI
You can view, create, and edit tasks all in one place from the Schedules page. This will provide a quick view of the schedule, when it's expected to run, and who has scheduled it. Cron jobs only show the tasks themselves (not who scheduled them). Editing, pausing, or starting scheduled tasks is also simple with Airplane. You can take a number of actions directly from the UI.
Manage and edit schedules centrally from the Schedules page.
Airplane provides a number of benefits that make it a simpler and safer alternative to cron. Airplane tasks are serverless, come with audit logs, permissions, and notifications, and can be used for scheduled tasks or for one-off operations that need to be run on-demand rather than on a schedule (such as testing your REST API).
In addition to job scheduling, Airplane can be used to automate one-off manual tasks, and for building powerful custom user interfaces and multi-step processes. Some common use cases include admin panels, customer onboarding workflows, approval flows, long-running jobs, feature flags, and much more.
If anything in this article caught your interest, you can get started with Airplane for free or say hello at firstname.lastname@example.org 👋.