Movatterモバイル変換


[0]ホーム

URL:


keyboard_arrow_downCategories
search
close

Mar 26, 2025

Linas L. & Aris S.

8min Read

What Is a Cron Job: Understanding Cron Syntax and How to Configure Cron Jobs

Copy link
Copied!
What Is a Cron Job: Understanding Cron Syntax and How to Configure Cron Jobs

Acron job is a task created usingcron, 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.

What Are Cron Jobs For?

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.

How Cron Jobs Work

Cronis adaemon – a background process executing non-interactive jobs. Acron file is a text file that contains commands to run periodically at a specific time.

Thecron table orcrontab 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.

While convenient, there are several limitations of cron jobs:

  • 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 theenvironment 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 usinganother 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

The syntax for crontab command

Before setting up cron jobs, you must understand cron’s syntax and formatting to ensure the script runs properly. Thecrontab syntax consists of five fields with the following possible values:

  • Minute. The minute of the hour the command will run, ranging from0-59.
  • Hour. The hour the command will run, ranging from0-23 in a 24-hour notation.
  • Day of the month. The date of the month the user wants the command to run, ranging from1-31.
  • Month. The month that the user wants the command to run. It ranges from1-12, representing January until December.
  • Day of the week. The day of the week for a command to run, ranging from0-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.

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 theMinute field to make the cron job run every minute.
  • Comma (,). An operator for listing multiple values. For example, writing 1,5 in theday-of-weekfield will schedule the job to run every Monday and Friday.
  • Hyphen (-). Users can determine a range of values. Write6-9 in theMonth 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 theHourfield.
  • Last(L). Users can use this operator in theday-of-month andday-of-week fields. For example, writing3Lin theday-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 the1stof a month is a Saturday, writing1Win theday-of-month field will run the command onMonday the3rd.
  • Hash(#). An operator for theday-of-week field that determines a specific day of the month, using a number between1 to5. For instance,1#2 means the second Monday of the month.
  • Question mark(?). This operator inputsno specific value for theday-of-month andday-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 as1-2/12. To learn more about operator usage, read thecron manual.

If you’re unsure about manually writing the cron syntax, use free tools likeCrontab Generator orCrontab.guru to generate the exact numbers for the time and date of your command.

Cron Syntax Examples

To help you better understand the cron syntax, here’s a list of sample commands to conduct system management with cron jobs:

ExampleExplanation
0 0 * * 0 /root/backup.shPerform a backup every Sunday at midnight.
0 * * * 1 /root/clearcache.shClear the cache every hour on Mondays.
0 6,18 * * * /root/backup.shBackup data twice a day at 6 am and 6 pm.
*/10 * * * * /scripts/monitor.shPerform monitoring every 10 minutes
*/15 * * * * /root/backup.shPerform a backup every 15 minutes.
* * 20 7 * /root/backup.shPerform a backup every minute on July 20.
0 0 * * 2 * /root/backup.shPerform a backup at midnight every Tuesday.
* * * 1,2,5 * /scripts/monitor.shPerform monitoring every minute in January, February, and May.
10-59/10 5 * * * /root/clearcache.shClear the cache every 10 minutes at 5 am, starting from 5:10 am.
0 8 1 */3 * /home/user/script.shMake the task run quarterly on the first day of the month at 8 am.
0 * * * * /root/backup.shCreate a backup every hour.
* * * * * /scripts/script.sh; /scripts/scrit2.shInclude multiple tasks in a single cron job. This is useful for scheduling multiple tasks to run at the same time.
@reboot /root/clearcache.shClear the server cache every time you turn on the system.
0 8 1-7 * 1 /scripts/script.shRun a script on the first Monday of each month at 8 am.
5 4 * * 0 /root/backup.shCreate a backup every Sunday morning at 4:05 am.
15 9 1,20 * * /scripts/monitor.shPerform monitoring at 9:15 am on the 1st and 20th of every month.
@hourly /scripts/monitor.shPerform monitoring every hour.
0 0 1,15 * 3 /scripts/script.shRun a script at midnight every Wednesday between the 1st and 15th of every month.
15 14 1 * * /root/clearcache.shClear the cache on the first day of every month at 2:15 pm.
15 6 1 1 * /root/backup.shPerform a backup every January 1st at 6:15 am.
0 0 * * * /scripts/monitor.shRun the monitoring script once a day at midnight.
0 0 15 * * /root/clearcache.shClear the cache at midnight on the 15th of every month.

Cron Job Special Strings

Special strings are used to quickly schedule cron jobs at certain time intervals without specifying the exact values. To use them, write a simple phrase starting with an@. Here are some useful special strings to use in commands:

  • @hourly. The job will run once an hour.
  • @dailyor @midnight. These strings will run the task every day at midnight.
  • @weekly. A string for scheduling tasks once a week at midnight on Sunday.
  • @monthly.This special string runs a command once on the first day of every month.
  • @yearly. Use this string to run a task once a year at midnight on January 1st.
  • @reboot. This string runs the cron job once during a system startup.

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.allowandcron.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.

How to Run Cron Jobs

In this section, we will show you how to schedule cron jobs by inputtingcommands into a shell program on a Linux-based system, such asHostinger’s VPS hosting.

Connect to your VPS using Terminal or an SSH client likePuTTY. Alternatively, Hostinger VPS users can access the command line interface (CLI) on their web browser viahPanel.

Pro Tip

In addition to VPS, Hostinger’smanaged hosting plans also support cron jobs. For example, the Business web hosting plan lets you schedule unlimited jobs.

After accessing hPanel, navigate to theVPSmenu from the sidebar and clickManageon your plan. Then, clickBrowser terminal.

The Browser terminal button on hPanel's VPS overview page

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 withapt:

sudo apt install cron

Before proceeding with the basic cron job operations, you must understand the configuration files – thesystem crontabanduser crontab.

Thesystem crontab is used to schedule system-wide essential jobs that are only editable by those with root privileges. Meanwhile, leverage theuser crontabtocreate 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.

A basic cron job operation

Using a Cron Job to Create a Crontab File

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 asnanoorvi. In the text editor, you can add other commands or edit existing ones.

Using Cron Job to Display a List of Active Scheduled Tasks

To see a list of active, scheduled tasks in your system, enter the following command:

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

Using Cron Jobs to Grant Yourself Root Access

Due to user privilege restrictions, some commands can only run using root permissions. To give yourself root privileges,attachsudo suto the beginning of the command.

For example, you needsudo suto run a crontab command that edits other users’ scheduled jobs:

sudo su crontab -u username -e

In addition, you can add cron jobs to theetc/cron.d directory to store automatic installation and update scripts. To add them to this directory, you must have root access and conform torun-parts naming conventions.

Alternatively, a root user can move their cron job scripts into the following directories to schedule their execution:

  • /etc/cron.hourly/. Run the script once an hour.
  • /etc/cron.daily/. Run it once a day.
  • /etc/cron.weekly/. Run it once a week.
  • /etc/cron.monthly/. Run it once a month.

Using a Cron Job to Delete Scheduled Tasks

To delete all scheduled tasks in the crontab entries and start from the beginning, type the following command:

crontab -r

Alternatively, use thecrontab -icommand. 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.

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.

Cron Job FAQ

In this section, we will answer several commonly asked questions about cron jobs to help you understand the tool better.

What Does a Cron Job Do?

Cron jobs are Linux commands for automating repetitive tasks on your server. It lets you schedule tasks for your system like updating, installing, or monitoring with a single command.

What Is the Use of * * * * * In Cron?

* * * * * is a cron schedule expression wildcard, meaning your cron job should run every minute, regardless of the hour, day, date, or month.

How Do I Run a Cron Job?

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 thecrontab -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.

Author
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.

Author
The Co-author

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 onLinkedIn.

Copy link
Copied!

Related tutorials

Tutorial post thumbnail

25 Apr • VPS Docker

How to install Docker using Ansible on Ubuntu

Installing Docker manually works fine – until you have to do it more than once. That’s precisely the kind of problem Ansible was made to solve....

Tutorial post thumbnail

25 Apr • VPS Docker

How to run Nginx in a Docker container: From image pull to configuration

Nginx is a high-performance web server often used as a reverse proxy, load balancer, and content cache. Its efficiency and flexibility have made it a...

Tutorial post thumbnail

18 Apr • VPS

How to change Minecraft version from Java to Bedrock on a dedicated server

Whether you’re aiming to support crossplay, reach more players, or just prefer Bedrock’s performance on certain devices, moving a Minecraft...

What our customers say

Trustpilot

Comments

Leave a comment
Author
faraz

August 03 2020

Reply

when i try to update any thing on my Wordpress website a message shown on screen" the link you followed is expired please try again "

Author
Hannah

September 27 2020

Reply

Are cron schedules made in cPanel relative to European time?

Author
Precious Onyewuchi

September 29 2021

Reply

Is there a way to implement this in django rest frameworks?

Author
Rohit

June 28 2022

Reply

How to pass parameters to crontab

Author
Martin B

April 28 2023

Reply

My Cron Job is working after discussion with Customer Service. The two problems with my original Cron Job may assist others;1. The file directive was incorrect. Was not working using public_html/MyScript.php but worked when changed to 'domains/Your Domain address/public_html/MyScript.php 2. Cron Jobs do not like files by reference and require an absolute path. My original MyScript.php script called supporting files by reference and did not work. For instance include_once('../include/config.inc.php'); Changed this to the absolute path and Cron Job worked. In this instance include_once('domains/Your Domain address/public_html/include/config.inc.php');

Author
Nicole

September 05 2023

Reply

Is there a way to set-up a crib job that runs on specific months? I have the premium hosting plan and I can’t seem to figure out how.

Author
nazim ali

November 30 2023

Reply

how to set 3 minutes cron job.

Leave a replyCancel reply

Please fill the required fields.Please accept the privacy checkbox.Please fill the required fields and accept the privacy checkbox.

Thank you! Your comment has been successfully submitted. It will be approved within the next 24 hours.

[8]ページ先頭

©2009-2025 Movatter.jp