Skip to main content
In this article, we explore how to schedule tasks to run at specified times on Linux systems. Automating tasks such as database backups every Sunday at 3:00 AM is crucial for consistent system maintenance. There are three primary tools available for task scheduling:
  1. Cron Utility
  2. Anacron
  3. At Utility
Below, we detail how each tool works and how to configure them for your needs.

Cron Utility

Cron is best suited for repetitive tasks that run at regular intervals—whether every few minutes, specific hours, days, or even months. The basic syntax for a cron job consists of five time-and-date fields followed by the command to be executed. When editing the system-wide cron table (found at /etc/crontab), a username field is included. The time fields are as follows:
  • Minute (0–59)
  • Hour (0–23)
  • Day of the month (1–31)
  • Month (1–12)
  • Day of the week (0–6, where 0 or 7 denotes Sunday)
You can use special characters in these fields:
  • Asterisk (*) denotes every possible value.
  • Comma (,) separates multiple values. For example, “15,45” in the minute field runs the job at minute 15 and 45.
  • Dash (-) specifies a range (e.g., “2-4” in the hour field).
  • Slash (/) defines steps. For example, ”*/4” in the hour field indicates every 4 hours, and “0-8/4” represents 0 AM, 4 AM, and 8 AM.
The default system-wide cron table, located at /etc/crontab, usually includes explanatory comments. Below is a sample excerpt:
The image illustrates a server setup for automated tasks, specifically a database backup every Sunday at 3:00 AM, using Cron, Anacron, and "at" for scheduling.
When modifying the system-wide cron file, be aware that package updates may overwrite your changes. It is generally safer to add cron jobs using the user’s personal cron table.
To edit your personal cron table, run:
In your personal cron table, the username is not required because the job inherits your current privileges. For example, if you want to create a file named test_path every day at 6:35 AM using the touch command, first determine its full path:
Then, add the following line using crontab -e:
Save and exit the editor to schedule the job. Below are additional examples for scheduling tasks:
To run a command every day at 3:00 AM or at the top of every hour (when the minute is 00), adjust the timing fields as needed. For further testing of your cron expressions, visit crontab.guru. To list your current cron jobs, use:
To view or edit other users’ cron jobs (e.g., root or another user), use sudo:
If you need to remove a user’s cron jobs entirely, use the -r option:
An alternative to adding jobs to the cron table directly is by placing scripts into specific directories, such as /etc/cron.daily, /etc/cron.hourly, /etc/cron.weekly, or /etc/cron.monthly. For instance, to schedule a shell script named shellscript to run hourly:
Make sure the script is both readable and executable. To remove it, simply delete the file from the directory.

Anacron

Anacron is designed for tasks that need to run periodically (daily, weekly, monthly) on systems that are not continuously powered on. If a scheduled cron task is missed because the system is off, anacron will run the task once the system is available. On many systems, anacron is pre-installed. If not, install it with:
Anacron’s configuration is in the /etc/anacrontab file, which includes helpful comments and examples. Here’s an excerpt:
The syntax in the anacrontab file consists of four fields:
  1. The period in days (e.g., 1 for daily, 7 for weekly).
  2. A delay (in minutes) after system startup.
  3. A unique job identifier.
  4. The command to execute.
For example, to run a job every three days with a 10-minute delay:
Other examples include:
After editing, verify your anacrontab syntax with:
If no errors are reported, your anacron entries are correctly formatted.
For more detailed information on anacron, refer to the man pages by running:

At Utility

The at utility is ideal for one-time task scheduling, rather than recurring jobs. If at is not already installed on your Ubuntu system, install it with:
To schedule a job with at, specify the desired run time. For example, to run a job at 3:00 PM:
You can also schedule jobs for specific dates or relative times. Some examples include:
To view scheduled at jobs, use:
Here, the first number is the job ID. To inspect a job’s details, use the -c option, similar to using cat. To remove a job, execute:
For more information on the at utility, refer to the at documentation.

Each of these tools—cron, anacron, and at—has its unique strengths. Cron excels for regular, recurring tasks; anacron ensures tasks are completed even if scheduled times are missed due to system downtime; and at is perfect for one-off commands. By mastering these scheduling utilities, you can automate and manage tasks efficiently on your Linux system, ensuring a more resilient and well-maintained environment.

Watch Video

Practice Lab