How to Use Cron on Linux: Tips, Tricks, and Examples

Your ultimate guide to cron jobs and crontab on Linux. From basics to advanced tips, learn everything you need for scheduling success.

Cron jobs are a fundamental yet powerful tool within the Linux operating system. These time-based job schedulers are indispensable for system administrators, developers, and tech enthusiasts alike, enabling the automation of routine tasks.

This article is designed to demystify cron jobs. It offers a comprehensive guide that explains what cron jobs are, how they work, and, most importantly, how you can use them to automate repetitive tasks on your Linux system.

So, whether your goal is to set up automatic backup files, run scripts at specific times, or automate your personal projects, this article will show you how cron jobs make it all possible. So, let’s dive in!

What is Cron?

Cron is a time-based job scheduler in Unix-like operating systems, including Linux and macOS. It enables users to schedule jobs (commands or scripts) to run periodically at fixed times, dates, or intervals.

It is most commonly used to automate system maintenance or administration tasks, but it can be used for any purpose that requires regular, scheduled command execution.

How Cron Works

The core component in the background is the cron daemon, named crond. Its primary job is to check for scheduled tasks and execute them when their specified time arrives.

The daemon wakes every minute to check for jobs in the crontab files or the directory-based configuration. This naturally brings us to the next critical aspect of Cron that underpins its operational setup – the files and directories it uses.

Cron Files and Directories

Crontab files are the heart of the cron job scheduling system. “Crontab” stands for “cron table,” as these files contain a list of commands to run at scheduled times.

Each line in a crontab file represents a separate job and includes information on when to run the job followed by the command to be executed.

Essentially, crontab files come in two varieties: those owned by individual users and the system-wide “/etc/crontab“. Below are the key details you should be aware of regarding both.

But before that, something important to clarify. The cron files belonging to individual users are not kept in their home directories but are instead found in the “/var/spool/cron” directory. At the same time, cron job files for system services and applications are typically placed in the “/etc/cron.d “.

User Crontabs

User crontabs are personal to each user on a system. Users can have their crontab file to schedule tasks under their user ID.

The primary advantage of user crontabs is that they allow individual users to manage their job schedules without requiring administrative privileges.

System-Wide Crontab

Unlike user-specific crontab files, “/etc/crontab” is a system-wide configuration file. It follows a slightly different format from user crontabs by including a user field that specifies the user account under which the command should run.

This allows system administrators to schedule jobs to run under any user without modifying that user’s crontab, allowing for greater flexibility in task management across different user accounts.

Generally, the “/etc/crontab” file is typically used for jobs that need to be run with administrative privileges or that are essential to the system’s operation.

Another important point is that while users can edit their crontab entries, the system-wide crontab can be edited directly only by a root user.

Cron’s Directories

In addition to crontab files, most Linux systems include a set of directories that are scanned by cron for scheduled jobs: “/etc/cron.daily,” “/etc/cron.hourly,” “/etc/cron.weekly,” and “/etc/cron.monthly.”

These directories allow for more straightforward scheduling of tasks that need to run regularly without specifying exact times in a crontab.

Scripts and executable files placed in these directories are run once per day, hour, week, or month, respectively. The exact time when scripts in these directories are executed is determined by the configuration found in “/etc/crontab” or the daemon’s configuration file, usually located in “/etc/cron.d/.”

Cron Syntax Basics

A cron job is defined by a line of text in a Cron file (crontab). Each line consists of a series of fields separated by spaces or tabs, followed by a command or a script to be executed. The basic syntax for a cron job is as follows:

minute hour day_of_month month day_of_week command_to_execute
The Structure of a Cron Job
The Structure of a Cron Job

Let’s delve into each component:

Minute (0-59)

This field specifies the minute at which the command will run. It can be a value from 0 to 59. For example, setting this to 0 would run the command at the beginning of the hour.

Hour (0-23)

The hour field is specified in a 24-hour format. It determines at which hour of the day the command is executed. For instance, setting this to 14 would run the command at 2 PM.

Day of Month (1-31)

This field specifies the day of the month the command will run. It can be any value from 1 to 31, depending on the number of days in the month. For example, setting this to 1 would run the command on the first day of each month.

Month (1-12)

The month field determines in which month the command will execute. It can be a value from 1 (January) to 12 (December). For instance, setting this to 12 would execute the command in December.

Day of Week (0-6)

This field specifies the day of the week the command should run. It can be a value from 0 (Sunday) to 6 (Saturday). For example, setting this to 5 would run the command every Friday.

Command or Script

Finally, the command or script field is where you specify what action the cron job should perform. This could be any command or a path to a script file that the Cron daemon will execute at the specified time.

Special Characters in Cron Syntax

Cron syntax also supports special characters to specify more complex scheduling patterns:

  • Asterisk (*): Represents “every” time unit. For instance, “*” in the hour field means “every hour.”
  • Comma (,): Allows specifying a list of values. For example, “1,3,5” in the day_of_week field means “run on Monday, Wednesday, and Friday.”
  • Hyphen (-): Specifies a range of values. For example, “9-17” in the hour field means “every hour from 9 AM to 5 PM.”
  • Slash (/): Specifies increments. For instance, “*/10” in the minute field means “every 10 minutes.”

Apart from these, the cron jobs have special shortcut strings that can replace the five fields for time and date. These shortcuts provide a quick way to specify the same schedule that would otherwise require a numeric representation.

StringMeaning
@rebootRuns the specified command once at startup.
@yearly,
@annually
Both run the specified task every year at 12:00 AM on the 1st of January. Equivalent to specifying “0 0 1 1 *”
@monthlyRuns the job once a month, on the 1st, at 12:00 AM. Equivalent to “0 0 1 * *”
@weeklyRuns the job once a week at 12:00 AM on Sunday. Equivalent to “0 0 * * 0”
@daily,
@midnight
Both run the cronjob every day at 12:00 AM. This is equivalent to specifying “0 0 * * *” in the crontab file.
@hourlyRuns the job at the top of every hour. Equivalent to “0 * * * *”

When Does a Cron Job Start?

The cron system runs a check on crontab every minute. It runs tasks when the set time (minutes, hours, and months) aligns with the actual time. Also, it checks the day: if the specific date in the month or the day of the week matches today, then it’s time to execute the command or script.

Cron Jobs and Paths

When a cron job runs, it does so in a restricted environment, which means it does not automatically inherit the user’s or system’s environment variables or paths. This can lead to issues where the cron job fails because it cannot find the necessary executables or scripts due to an undefined or incorrect path.

In light of this, you can explicitly set the PATH environment variable within the crontab file to handle paths in cron jobs. This tells the cron daemon where to look for executables. For example:

PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/binCode language: JavaScript (javascript)

Adding this line at the beginning of your crontab file ensures that all cron jobs below will search these directories for executables.

However, taking into account and following best practices, we recommend always specifying absolute paths to all executables and scripts in your cron jobs to avoid any ambiguity and potential errors.

Furthermore, before relying on the automated execution, manually run your scripts or commands using the same paths and environment settings defined in your crontab to ensure they work as expected.

Setting Up and Managing Cron Jobs

Okay, let’s set aside the theory now. You’re equipped with all the essential knowledge about how cron jobs function and their components. It’s time to dive into the exciting part: creating a cron job task.

Create User’s Cron Job

To create or edit your user’s crontab file, use the following command in the terminal:

crontab -e

It’s the standard way for users to create and edit cron jobs. When executed, it edits the cron jobs for the user who runs the command by opening your user’s crontab file in the default text editor set for your system, such as Vi, Nano, or another one.

If it’s your first time using the command, you may be prompted to select an editor before proceeding.

Choosing the Crontab Editor
Choosing the Crontab Editor

Next, to add a cron job, simply add a new line to your crontab file following the syntax explained above. For example, we’ll schedule a backup script, “backup.sh,” located in our user’s home directory, to run daily at 3:00 AM, by adding the following line:

0 3 * * * /home/linuxiac/backup.shCode language: JavaScript (javascript)
Creating a Cron Job
Creating a Cron Job

Numerous lines prefixed with the “#” symbol and thus commented out at the start of the file serve as concise guides for setting up a cron job. Type yours after them, as shown in the image above.

Next, save and exit the editor. Remember, the Cron service automatically checks for changes to crontab files and applies them accordingly, so you don’t need to restart it after making changes.

In closing, it’s worth noting that using crontab -e offers the added benefit of automatically checking the syntax when you save and exit the file. Cron will alert you to any errors detected, providing a valuable safeguard against accidentally entering invalid cron jobs.

Create a System-Wide Cron Job

Since crontab -e focuses on individual user crontabs, it’s not designed for system-wide Cron configuration changes that might be necessary for certain administrative tasks.

If this is the case, directly editing the “/etc/crontab” file gives you system-wide crontab capabilities. Unlike user-specific crontabs, this file can include system-wide tasks and supports specifying the user for each task, offering flexibility for running commands under different user accounts.

For example, let’s create a system-wide cron job that will delete all files with the “.log” extension from the “/var/log/myservice” directory at 2:00 AM.

To do this, we first open the “/etc/crontab” file with our preferred terminal editor:

sudo vim /etc/crontab

Then we enter the cron job, which looks like this:

0  2  *  *  * root /usr/bin/find /var/log/myservice -type f -name '*.log' -deleteCode language: JavaScript (javascript)
Create a system-wide Cron job.
Create a system-wide Cron job.

What sets this apart from user cron jobs is the inclusion of an extra column following the initial five-time fields. This column allows for the specification of the user account under which the job will execute – in this case, root.

However, directly editing the “/etc/crontab” file has two main drawbacks. First, this approach does not provide syntax checking, increasing the risk of errors.

The second one is because “/etc/crontab” affects the entire system; incorrect entries can have widespread implications, potentially affecting system stability or security. So, use it with caution.

Listing User Cron Jobs

To ensure that your cron jobs have been scheduled correctly, you can display your user’s crontab file content using:

crontab -l
Listing user's Cron jobs
Listing user’s Cron jobs

This command lists all cron jobs scheduled for your user, allowing you to verify or review the tasks set to run.

Listing System-wide Cron Jobs

As we already know, the system-wide cron jobs are stored in different locations and are not listed in the user’s crontab. To list system-wide cron jobs, you will need to look into the “/etc/crontab” file and the directories “/etc/cron.d/,” “/etc/cron.daily/,” “/etc/cron.hourly/,” “/etc/cron.weekly/,” and “/etc/cron.monthly/“.

You can view these files using the cat command or any text editor. For example:

sudo cat /etc/crontab

To list contents of directories, for example, “/etc/cron.daily,” use:

ls /etc/cron.daily/
Listing system-wide Cron jobs
Listing system-wide Cron jobs

Listing Cron Jobs for Other Users

If you have superuser (root) privileges, you can list cron jobs for any user on the system by using the crontab command with the “-u” option followed by the username and the “-l” option. For example, to list cron jobs for a user named “bobby,” you would run:

sudo crontab -u bobby -l

This command is handy for system administrators managing cron jobs across multiple user accounts.

Editing Cron Jobs

To edit a cron job, similarly to creating, use the following command to open the current user’s crontab file in the default editor:

crontab -e

When you open the crontab file, navigate to the line containing the cron job you wish to edit and modify the schedule or command as needed, then save and exit the file.

If you need to edit the crontab file of another user (assuming you have the necessary permissions), for example, of user “bobby,” use:

sudo crontab -u bobby -e

Deleting the Cron Job

Depending on your goal, you have several methods for removing a cron job. To delete only a specific job, use the crontab -e command to open the crontab file.

From there, navigate to the line representing the job you want to delete. Remove that line by deleting it entirely. At the same time, be careful to keep all other lines the same. After that, save your changes and exit the editor.

However, if you wish to delete all scheduled cron jobs for a user, you can remove the user’s crontab file. This action will erase all scheduled tasks, so it should be done cautiously.

Open the terminal and enter the command shown below:

crontab -r
Delete the user's crontab
Delete the user’s crontab

This will remove the current user’s crontab file without a confirmation prompt, so ensure you want to proceed before executing it.

If you prefer to receive a confirmation prompt before deleting, use the command crontab -i followed by -r. This will ask for confirmation before removing the crontab file.

Crontab Cheatsheet

To summarize the key points discussed above regarding the crontab command’s primary options, they are briefly outlined in the table below.

CommandDescription
crontab -eEdit the crontab file or create one if it doesn’t already exist.
crontab -lDisplaying the content of crontab file.
crontab -rRemove the entire crontab file.
crontab -u userWhen paired with additional options, this feature enables the modification or viewing of a user’s crontab file, a capability reserved exclusively for users with admin privileges.

How to Troubleshoot Cron Jobs

Unfortunately, when a cron job fails to run, it can be frustrating and potentially problematic, depending on the task. So here are some basic guidelines for investigating the cause of this.

The first step in troubleshooting is to ensure that the cron job is correctly defined. Verify that each field is specified correctly according to your requirements. A common mistake is incorrect syntax or misunderstanding how cron interprets special characters and ranges.

Then, ensure the Cron daemon is running on your system. You can check this by running a command like:

sudo systemctl status cron
 Check if the Cron service is running.
Check if the Cron service is running.

Ensure the script or command your cron job tries to run has the appropriate permissions and that you use absolute paths. Cron jobs run in a limited environment with a minimal PATH definition, so specifying the full path to any command or script is crucial.

Additionally, the file must be executable and accessible to the user under which the cron job is scheduled. Try running the command or script manually from the command line using the same user account the cron job uses.

This can help you verify that the command works as expected without the cron environment. If the command fails, you’ll know the issue lies with the command or script rather than cron.

Remember that cron jobs run in a non-interactive, non-login shell environment, which means they might not have access to the same environment variables as when you run commands manually.

If your script relies on environment variables, you might need to explicitly set them at the beginning of your script or within the cron job definition.

Cron Job Examples

Finally, we will present several cron job examples that cover nearly all potential Cron syntax situations. Consider these as a foundation for crafting your own.

CommandExplanation
* * * * *Run the cron job every minute.
0 * * * *Run the cron job every hour.
0 0 * * *Run the cron job every day at midnight.
0 2 * * *Run the cron job at 2 AM every day.
0 0 15 * *Run the cron job every 15th of the month at midnight.
0 0 0 12 *Run the cron job on Saturday at midnight.
0 0 * * 6Run the cron job at 3 PM daily from Monday through Friday.
0 15 * * 1-5Run the cron job at 3 PM daily from Monday through Friday.
*/5 * * * *Run the cron job every 5 minutes.
0 8-16 * * *Execute the cron job daily, hourly, on the dot, from 8 AM to 4 PM.
0 4 * * 2,4Run the cron job at 4 AM on Tuesday and Thursday.
@rebootRun the cron job when the system starts.

Furthermore, online tools such as this, this, or this offer robust web-based interfaces for setting up cron tasks.

Best Practices and Tips

  • Test your scripts manually before scheduling them with Cron to ensure they work as expected.
  • Use absolute paths for commands and scripts within the crontab file to avoid path issues.
  • Redirect output to a file or mail to capture any output or errors for troubleshooting (“command > /path/to/logfile 2>&1“). This way, you can review the log file for any issues.
  • Be mindful of system load when scheduling jobs, especially if they are resource-intensive or if you have many jobs scheduled around the same time.

Conclusion

Cron jobs are essential to Linux system administration. They offer a robust and versatile tool for scheduling and automating tasks.

Its beauty lies in its versatility and precision. From simple commands executed at minute intervals to complex scripts scheduled for specific dates and times, cron adapts to many requirements.

With user and system-wide crontab files alongside directories designated for periodic tasks, cron jobs offer flexibility for individual users and system administrators to tailor task scheduling to their specific needs.

By leveraging the insights and examples in this article, you’re well on your way to achieving scheduling success, enhancing productivity, and unlocking new possibilities in your Linux journey.

For additional details, the cron and crontab manual pages contain thorough information and explanations on how the cron system functions.

Thanks for your time! Your feedback and comments are, as always, most welcome.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%

Leave a Reply

Your email address will not be published. Required fields are marked *