We often run into situations that require us to run a piece of code periodically—for example, renewing SSL certificates that may expire every year or two. Fortunately, most servers and operating systems provide some means to conduct repetitive tasks automatically. One popular solution is using cron.
A cron job, or simply cron, is a job scheduler that you set up on a machine or server to perform tasks at a specific interval. For example, a cron job might execute a script that sends emails to users on a specific day. Since these jobs are highly customizable, you can do things like set a cron job to run every 60 minutes or only run on Saturdays and Sundays.
In this post, we’ll cover what cron is and how to start, stop, and restart cron jobs.
What are cron jobs?
Cron jobs are tasks that run automatically following some predefined schedule. You can set the schedule for a cron job using the terminal on a Linux operating system. However, if you're setting up a cron job on a server, you'll need to SSH into the server first.
Under the hood, the cron daemon checks the crontab file for any active tasks. Crontab is used for setting up cron jobs.
You can view the content of the crontab file by running the following command:
After running the command, you should get an output very similar to this:
The lines that start with # represent comments. We’ll discuss crontab in more detail later on in this post.
Uses of cron jobs
Cron jobs serve a wide range of applications including:
Automation of system maintenance tasks
System maintenance tasks include things like deleting old log files to save storage space or checking for system updates regularly to ensure system software is up to date and compliant. You can use cron jobs to schedule tasks that perform both of these actions. For example, your cron job may run every week or once a month to delete old log files depending on how fast your files are growing.
Periodic backups of files and databases
Making frequent backups for files and databases on a server can greatly reduce the risk of data loss. However, making these backups manually can be repetitive and lead to inconsistencies. Periodic backups can be automated by creating a cron job that runs a script at a specific interval. For instance, we can set up a cron job that generates backups at 11 p.m. every day.
Sending email notifications
Using a cron job, you can automatically send emails at specific time intervals to notify customers about various events. For example, I might send a monthly email notifying customers of services that are about to expire.
Other repetitive tasks
Cron jobs might also be used to reduce the burden of other repetitive tasks such as daily operations (ex: running a daily report) and syncing data and systems at a regular cadence. For example, “sync new rows on this dataset every Monday at 10:00am”.
Overall, using cron jobs can save time and increase consistency in repetitive tasks.
How to create a cron job
Before we jump into how to create a cron job, let’s take a look at what a crontab is.
A crontab is a tool for creating and editing tasks in a cron job. Using the crontab tool, you can create a new cron job on a Linux machine. Crontab supports several commands that can be run through the terminal.
Creating a new cron job
To create a new cron job, run the following command in the terminal which edits the crontab file:
The terminal will then prompt you with a screen to select your preferred text editor. You may not get this prompt if a default editor is already set for your current user. The available options include vim, nano, and ed.
Using your preferred editor, enter the following code, then save:
44 3 * * * echo "hello world" > hello.txt
With that, you've successfully created your first cron job. This job will save the text "hello world" into a hello.txt file on your current directory. The part of the command before echo contains the actual definition of when the task should run. That is, the operation will occur every day at 3:44 a.m.
In the next section, we'll take a closer look at the various parts of the above cron command.
Understanding cron syntax
The below screenshot contains comments that describe the cron job syntax.
Let’s revisit our command from the previous section to walk through the parts of a cron command.
44 3 * * * echo "hello world" > hello.txt
The above command consists of six fields. Each field is separated from the next one using a space. The format for the fields are as follows:
minute(s) hour(s) day(s) month(s) weekday(s) command(s)
In our example, the value for minute is 44, and the value for hour is 3. The * sign means always. Using the * value for the day field means every day, and using the * value for the month field means every month. Setting the value for weekday to * means the job will run every day from Sunday to Saturday.
The sixth field provides the actual job or command to be executed at the scheduled time. In the case of our example, the command is echo "hello world" > hello.txt.
How to start, stop, and restart a cron job
Start a cron job
A cron job is started the moment it is added to the crontab. Note that the task may fail to run if the cron daemon isn’t started. To start the cron service on your Linux machine, run one of the following commands, depending on your Linux distro.
Red Hat / CentOS:
service crond start
sudo service cron start
sudo /etc/init.d/cron start
Stop a cron job
You can stop a single cron job by removing its line from the crontab file. To do that, run the crontab -e command and then delete the line for the specific task. Alternatively, you can stop the cron job by commenting it out in the crontab file.
To stop all cron jobs at once and maybe resume them later, you can stop the cron daemon using the following commands:
Red Hat / CentOS:
service crond stop
sudo service cron stop
sudo /etc/init.d/cron stop
Restart a cron job
To restart the cron daemon, run the following commands:
Red Hat / CentOS:
service crond restart
sudo service cron restart
sudo /etc/init.d/cron restart
Tip: You may need to add or uncomment any cron jobs you previously stopped by editing the crontab.
Additionally, restarting a cron job or the cron service may clear some of the issues that prevent your cron jobs from running.
Using Airplane as alternative to cron for schedules
Although cron is one of the most common ways that developers run tasks on a recurring basis, it has its own set of limitations and is often difficult to use. For example, you need to be familiar with operating a machine using the Linux terminal and cron is often high maintenance, requires manual up-keep, lacks logging, and there’s no central place to see all the cron jobs you have running.
You might consider replacing cron with Airplane Schedules. Airplane is a fully-managed SaaS that abstracts away many of the difficult parts of maintaining cron jobs at scale. Engineers use Airplane as a simpler, safer, and more feature-rich job scheduler compared to alternatives like cron, Airflow, Heroku, or other enterprise schedulers. Creating a schedule in Airplane takes just minutes and you can get started for free.
Airplane schedules are effective and easy-to-use; they:
- Automatically generate audit logs
- Support nuanced permissions
- Provide customizable notifications (can be sent via Slack, email, or in app)
- Allow you to see and update configuration on scheduled tasks from the UI
- Don't require any maintenance after setup
Airplane also does a lot more than just schedules, providing a developer platform for building a wide variety of internal tools and workflows.
If you're interested in trying out Airplane, you can watch a quick demo and sign up for a free account here.