Cron Job - What It Is and How To Configure It in 2024
Cron Job - What It Is and How To Configure It in 2024
What Is a Cron Job: Understanding Cron Syntax and How to Configure Cron Jobs
VPS
What Is a Cron Job: Understanding Cron Syntax and How to Configure Cron Jobs
A cron job is a task created using cron, a tool for scheduling and automating future tasks on
Unix-like operating systems.
Setting up cron jobs is highly beneficial as you won’t need to perform repetitive tasks
manually, ensuring efficiency and minimal human error.
In this article, we will explain the basics of cron jobs, their types, syntax, special strings, and
permissions. We will also share cron job best practices and provide command examples to
help you understand how to use cron jobs.
With cron jobs, system administrators can automate maintenance, disk space monitoring,
and backups at regular intervals. This makes cron jobs ideal for computers that work 24/7,
such as a virtual private server. They are also useful for web developers as they can set up
simultaneous cron jobs at different intervals to back up a site, check for broken links, and
clear its cache.
The cron table or crontab configuration file is /etc/crontab by default. Only system
administrators can edit the system crontab file. However, since Unix-like operating systems
support multiple admins, users can create their own files to schedule specific jobs.
https://www.hostinger.com/tutorials/cron-job 1/9
7/14/24, 10:00 PM Cron Job: What It Is and How to Configure It In 2024
The shortest interval between jobs is 60 seconds. Users can only set the cron job
interval settings to one minute or more.
Missed jobs need a manual reset. Admins can’t distribute cron jobs to multiple
computers on a network. So, if the computer’s cron crashes, the scheduled tasks won’t
execute. You must restart the missed jobs manually.
No auto-retry mechanism. Cron is designed to run at a given schedule. If a task fails,
it won’t run until the next scheduled time. This makes cron unsuitable for incremental
tasks.
No environment variables. Crontab can’t read the environment variables from several
files containing configuration data that is required to run some applications properly.
If you want to automate a one-time job, we recommend using another scheduling method
instead.
Pro Tip
Before creating a cron job, ensure your script works. To do that, open the file in your browser
by URL or execute it via SSH, depending on the script’s type. If it doesn’t work, contact your
hosting provider’s support team for help.
Crontab Syntax
Before setting up cron jobs, you must understand cron’s syntax and formatting to ensure the
script runs properly. The crontab syntax consists of five fields with the following possible
values:
Minute. The minute of the hour the command will run, ranging from 0-59.
Hour. The hour the command will run, ranging from 0-23 in a 24-hour notation.
Day of the month. The date of the month the user wants the command to run, ranging
from 1-31.
Month. The month that the user wants the command to run. It ranges from 1-12,
representing January until December.
Day of the week. The day of the week for a command to run, ranging from 0-6. The
value represents Sunday-Saturday. In some systems, the value 7 represents Sunday.
Important! Be careful when scheduling cron jobs for different time zones, and make sure
your configuration is correct.
https://www.hostinger.com/tutorials/cron-job 2/9
7/14/24, 10:00 PM Cron Job: What It Is and How to Configure It In 2024
In addition to the syntax, you must understand the cron job operators to modify the value in
each field. You must properly use these operators in all crontab files to ensure your
commands run:
Asterisk (*). This operator signifies all possible values in a field. For example, write an
asterisk in the Minute field to make the cron job run every minute.
Comma (,). An operator for listing multiple values. For example, writing 1,5 in the day-
of-week field will schedule the job to run every Monday and Friday.
Hyphen (-). Users can determine a range of values. Write 6-9 in the Month field to set
up a cron job from June to September.
Separator (/). This separator divides a value. If you want to run a script every twelve
hours, write */12 in the Hour field.
Last (L). Users can use this operator in the day-of-month and day-of-week fields. For
example, writing 3L in the day-of-week field means the last Wednesday of the month.
Weekday (W). An operator that determines the closest weekday from a given time. For
example, if the 1st of a month is a Saturday, writing 1W in the day-of-month field will
run the command on Monday the 3rd.
Hash (#). An operator for the day-of-week field that determines a specific day of the
month, using a number between 1 to 5. For instance, 1#2 means the second Monday
of the month.
Question mark (?). This operator inputs no specific value for the day-of-month and
day-of-week fields. It’s typically replaced with the cron daemon start-up time.
Pro Tip
In Vixie cron, you can combine separators with ranges to specify step values, such as 1-
2/12. To learn more about operator usage, read the cron manual.
If you’re unsure about manually writing the cron syntax, use free tools like Crontab
Generator or Crontab.guru to generate the exact numbers for the time and date of your
command.
https://www.hostinger.com/tutorials/cron-job 3/9
7/14/24, 10:00 PM Cron Job: What It Is and How to Configure It In 2024
Example Explanation
0 8 1 */3 * Make the task run quarterly on the first day of the
/home/user/script.sh month at 8 am.
@reboot /root/clearcache.sh Clear the server cache every time you turn on the
system.
0 8 1-7 * 1 /scripts/script.sh Run a script on the first Monday of each month at 8 am.
https://www.hostinger.com/tutorials/cron-job 4/9
7/14/24, 10:00 PM Cron Job: What It Is and How to Configure It In 2024
15 14 1 * * /root/clearcache.sh Clear the cache on the first day of every month at 2:15
pm.
Cron Permissions
Ensure to set proper permissions for your system’s cron files to allow the jobs to run. You
can create or edit two files to set the permissions – cron.allow and cron.deny.
If /etc/cron.allow exists, it should contain a username permitted to run the cron job
automation. However, if your system has /etc/cron.deny containing a username, that
account can’t use cron.
Connect to your VPS using Terminal or an SSH client like PuTTY. Alternatively, Hostinger
VPS users can access the command line interface (CLI) on their web browser via hPanel.
Pro Tip
In addition to VPS, Hostinger’s managed hosting plans also support cron jobs. For example,
the Business web hosting plan lets you schedule unlimited jobs.
https://www.hostinger.com/tutorials/cron-job 5/9
7/14/24, 10:00 PM Cron Job: What It Is and How to Configure It In 2024
After accessing hPanel, navigate to the VPS menu on the top bar and select your plan.
Then, click on Browser terminal.
Cron is commonly pre-installed by default in all Linux distributions. Otherwise, run the
installation command according to your package manager. Here’s the command for Ubuntu
with apt:
Before proceeding with the basic cron job operations, you must understand the configuration
files – the system crontab and user crontab.
The system crontab is used to schedule system-wide essential jobs that are only editable
by those with root privileges. Meanwhile, leverage the user crontab tocreate and edit jobs
that only apply at the user level.
To edit the system crontab, ensure the current user has root privileges. Read on to learn
several basic operations that cron can perform.
Enter the snippet below into the command line to edit an existing crontab file. If your system
doesn’t have it, the command will automatically create a new one.
crontab -e
When entering crontab -e for the first time, it will ask you to choose which text editor you
want to edit the file with, such as nano or vi. In the text editor, you can add other commands
or edit existing ones.
crontab -l
If your system has multiple users, you can view their crontab file lists by entering the
command below as a superuser:
crontab -u username -l
https://www.hostinger.com/tutorials/cron-job 6/9
7/14/24, 10:00 PM Cron Job: What It Is and How to Configure It In 2024
For example, you need sudo su to run a crontab command that edits other users’ scheduled
jobs:
In addition, you can add cron jobs to the etc/cron.d directory to store automatic installation
and update scripts. To add them to this directory, you must have root access and conform to
run-parts naming conventions.
Alternatively, a root user can move their cron job scripts into the following directories to
schedule their execution:
To delete all scheduled tasks in the crontab entries and start from the beginning, type the
following command:
crontab -r
Alternatively, use the crontab -i command. It is similar to the previous one, except you will
get a confirmation option before removing the crontab:
crontab -i
Conclusion
Cron daemon is a service in a Unix-based system that lets you create automation scripts for
scheduling tasks. Meanwhile, cron jobs are the tasks automated using this tool, such as
updating, installing, or monitoring a system.
To automate tasks, write the crontab command in your system’s cron file. The command
contains the script for execution and five asterisks referring to the cron job’s execution time.
Change the value of these asterisks and use the operators to modify the time.
https://www.hostinger.com/tutorials/cron-job 7/9
7/14/24, 10:00 PM Cron Job: What It Is and How to Configure It In 2024
To run a cron job, connect to your Linux operating system using Terminal, an SSH client, or
another CLI application with root permission. Then, create a crontab file and add the script
using a text editor like Nano.
Log in to your server as the root user via SSH using PuTTY, Terminal, or Hostinger’s built-in
Browser terminal. Then, create a file using the crontab -e command.
Choose an editor to view the file and add your cron job script to the blank crontab file. Save
the file once you are done to enable the automation.
The author
Linas L.
Linas started as a customer success agent and is now a full-stack web developer and
Technical Team Lead at Hostinger. He is passionate about presenting people with top-notch
technical solutions, but as much as he enjoys coding, he secretly dreams of becoming a rock
star.
The Co-author
https://www.hostinger.com/tutorials/cron-job 8/9
7/14/24, 10:00 PM Cron Job: What It Is and How to Configure It In 2024
Aris Sentika
Aris is a Content Writer specializing in Linux and WordPress development. He has a passion
for networking, front-end web development, and server administration. By combining his IT
and writing experience, Aris creates content that helps people easily understand complex
technical topics to start their online journey. Follow him on LinkedIn.
https://www.hostinger.com/tutorials/cron-job 9/9