Ubuntu: How to create a cron job (that runs repeatedly at a set interval)

Updated: January 28, 2024 By: Guest Contributor Post a comment

Introduction

Managing tasks on a server is an essential skill for any system administrator or developer. One of the most powerful and commonly used tools for task automation on UNIX and Linux-based systems is cron. Whether you’re running a web server, automating backups, or just want to send out a weekly email reminder, cron jobs can help you schedule tasks to run automatically at specific intervals.

Cron is a time-based job scheduler in Unix-like operating systems, which uses the crontab command to manipulate the job entries. Each user has their own crontab file, and permissions dictate which users can schedule tasks. The syntax might look daunting at first, but it’s rather straightforward once you understand the components.

Getting Started with Cron Jobs

Before creating your first cron job, you need to open the terminal on your Ubuntu system. You can add a new job to your crontab file with:

crontab -e

This command will open the crontab file in your default editor. If you haven’t selected an editor before, you will be prompted to choose one, like vi or nano.

Basic Cron Syntax

* * * * * command_to_execute

The asterisks represent, in order:

  • Minute (0 – 59)
  • Hour (0 – 23)
  • Day of the month (1 – 31)
  • Month (1 – 12)
  • Day of the week (0 – 7, where both 0 and 7 mean Sunday)

Following the asterisks, you specify the command to run. Remember, each user’s crontab will run commands as that user, so set permissions accordingly.

First Cron Job Example

As an example, let’s say you want to create a backup of a directory every day at 1 AM. You would write a line like this in your crontab:

0 1 * * * /path/to/backup_script.sh

After saving and exiting the crontab file, your scheduled job will run with the specified command at the specified time.

Intermediate Cron Job Scheduling

Sometimes you might need to schedule a command to run every few minutes, hours, or on certain days. The following sections provide examples for different scheduling scenarios.

Every Five Minutes

*/5 * * * * /path/to/script.sh

Every Monday at 2:15 PM

15 14 * * 1 /path/to/weekly_update.sh

Every Six Hours

0 */6 * * * /path/to/six_hour_task.sh

Advanced Scheduling

In more complex scenarios, you might need to schedule commands based on environmental conditions, randomization, or job dependency. Advanced scheduling could involve shell scripting in conjunction with cron jobs.

Conditional Scheduling

If you need a job to run only under certain circumstances, you can use shell scripting to test for the condition before executing the task.

* * * * *  if [ "$(date '+
%a')" = "Mon" ]; then /path/to/monday_special.sh; fi

Randomized Intervals

When you need to avoid running tasks at predictable intervals—such as with system updates—you can use the sleep command in conjunction with rand.

@hourly sleep $((RANDOM\n\
%3600)) && /path/to/randomly_timed_job.sh

Error Handling and Troubleshooting

Errors in cron jobs can easily go unnoticed since jobs often run in the background with no user interaction. Redirecting output to a log file is a simple way to keep an eye on cron’s activities and any potential errors that arise during execution.

Logging Cron Job Output

30 2 * * * /path/to/daily_task.sh > /path/to/log_file 2>&1

Managing and Listing Crontab Entries

As your list of cron jobs grows, you can manage them with various crontab commands.

Listing Current Cron Jobs

crontab -l

Shows all cron jobs scheduled for the current user.

Removing All Cron Jobs for a User

crontab -r

Caution: This will delete all scheduled tasks without confirmation.

Conclusion

Scheduling tasks in Ubuntu is streamlines with cron jobs. Whether you’re dealing with simple daily operations or complex, conditional tasks, cron offers the flexibility and precision needed for efficient automation. With the knowledge to create, manage, and troubleshoot cron jobs, your administrative tasks can become smoother and more reliable, leaving you time to focus on more important things.