Table of Contents
Recommend some articles related to this article for you:
Home System Tutorial LINUX Learn how to configure scheduled tasks in Linux: using cron and anacron

Learn how to configure scheduled tasks in Linux: using cron and anacron

Jan 15, 2024 am 11:33 AM
linux linux tutorial Red Hat linux system linux command linux certification red hat linux linux video

Introduction In this article, we will explain cron and anacron and show you how to set up anacron in Linux. We will also compare these two tools.

Linux 中怎么设置计划任务:cron 与 anacron

cron settings in Linux

cron - is a daemon process used to run scheduled tasks such as system backups, updates, etc. It is suitable for scheduled tasks that run on machines that run 24X7, such as servers.

Commands/scripts are written in cron task scripts, which are scheduled in crontab files. The system default cromtab file is /etc/crontab, but each user can also create their own cromtab file to run user-defined commands at specific times.

To create a personal crontab file, just enter:

$ crontab -e
Copy after login

Linux 中怎么设置计划任务:cron 与 anacron

How to set up anacron in Linux

anacron is used to run commands at a frequency in days. It works slightly differently from cron in that it assumes the machine won't be on all the time.

Cron is also suitable for running daily, weekly and monthly scheduled tasks on machines that do not run 24X7, such as laptops and desktop computers (LCTT translation: not suitable for executing tasks by hours and minutes).

Suppose you have a scheduled task (such as a backup script) that you want to run every day using cron in the middle of the night, maybe while you are asleep and your desktop/laptop is shut down by then. Your backup script will not be run.

However, if you use anacron, you can ensure that the backup script will be executed the next time you turn on your desktop/laptop.

How anacron works on Linux

anacron tasks are listed in /etc/anacrontab, and tasks can be scheduled using the following format (comments in the anacron file must start with #).

period   delay   job-identifier   command
Copy after login

From the format above:

  • period - This is the frequency of the task, specified in days, or @daily, @weekly, @monthly Stands for daily, weekly, monthly. You can also use numbers: 1 - every day, 7 - every week, 30 - every month, or N - days.
  • delay - This is the number of minutes to wait before executing a task.
  • job-id - This is the unique name of the job written in the log file.
  • command - This is the command or shell script to be executed.

To browse the sample files, enter:

$ ls -l /var/spool/anacron/
total 12
-rw------- 1 root root 9 Jun  1 10:25 cron.daily
-rw------- 1 root root 9 May 27 11:01 cron.monthly
-rw------- 1 root root 9 May 30 10:28 cron.weekly
Copy after login

This is what actually happened:

  • anacron 会检查任务是否已经在 period 字段指定的时间被被执行了。如果没有,则在等待 delay 字段中指定的分钟数后,执行 command字段中指定的命令。
  • 一旦任务被执行了,它会使用 job-id (时间戳文件名)字段中指定的名称将日期记录在 /var/spool/anacron 目录中的时间戳文件中。

现在让我们看一个例子。这个会每天运行 /home/aaronkilik/bin/backup.sh 脚本:

 @daily    10    example.daily   /bin/bash /home/aaronkilik/bin/backup.sh
Copy after login

当机器在 backup.sh 期望被运行时是关机的,anacron 会在机器开机十分钟之后运行它,而不用再等待 7 天。

这里有两个你应该理解的 anacrontab 文件的重要变量:

  • START_HOURS_RANGE - 这个设置任务开始运行的时间范围(也就是任务只在这几个小时内运行)。
  • RANDOM_DELAY - 这定义添加到用户定义的任务延迟的最大随机延迟(默认为 45)。

这是你的 anacrontab 文件可能看上去的样子。

Anacron – /etc/anacrontab

# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
HOME=/root
LOGNAME=root
# These replace cron's entries
1       5       cron.daily      run-parts --report /etc/cron.daily
7       10      cron.weekly     run-parts --report /etc/cron.weekly
@monthly        15      cron.monthly    run-parts --report /etc/cron.monthly
@daily    10    example.daily   /bin/bash /home/aaronkilik/bin/backup.sh                                                                      
Copy after login

下面是 cron 以及 anacron 的比较,帮助你理解何时用他们其中一个。

cron anacron
它是守护进程 它不是守护进程
适合服务器 适合桌面/笔记本电脑
可以让你以分钟级运行计划任务 只能让你以天为基础来运行计划任务
关机时不会执行计划任务 如果计划任务到期,机器是关机的,那么它会在机器下次开机后执行计划任务
普通用户和 root 用户都可以使用 只有 root 用户可以使用(使用特定的配置启动普通任务)

The main difference between cron and anacron is that cron can run effectively on machines that are continuously running, while anacron is targeted at machines that will shut down within a day or a week.

If you know of other methods, please share them with us in the comment box.

Original address: https://www.tecmint.com/cron-vs-anacron-schedule-jobs-using-anacron-on-linux/‎

This article’s address: https://www.linuxprobe.com/cron-anacron-work.htmlEditor: Zhang Xiong, Reviewer: Pang Zengbao

Original address of this article: https://www.linuxprobe.com/cron-anacron-work.htmlEditor: Problem Terminator, Auditor: None

Recommend some articles related to this article for you:

  • Usage Examples of Zypper Package Manager for SUSE Linux
  • Swift Notes Variable Explanation
  • 《Angular Development Practice" pdf e-book free download
  • RSS syntax overview
  • "Spark Rapid Big Data Analysis 2nd Edition" pdf e-book free download
  • Install Memcached on Ubuntu
  • Multiple uses of awk commands
  • Basic use of Go generics
  • How to use Python or Bash to dynamically generate Jekyll configuration files
  • Let Linux maintain accurate time

The above is the detailed content of Learn how to configure scheduled tasks in Linux: using cron and anacron. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

deepseek web version entrance deepseek official website entrance deepseek web version entrance deepseek official website entrance Feb 19, 2025 pm 04:54 PM

DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

How to install deepseek How to install deepseek Feb 19, 2025 pm 05:48 PM

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

BITGet official website installation (2025 beginner's guide) BITGet official website installation (2025 beginner's guide) Feb 21, 2025 pm 08:42 PM

BITGet is a cryptocurrency exchange that provides a variety of trading services including spot trading, contract trading and derivatives. Founded in 2018, the exchange is headquartered in Singapore and is committed to providing users with a safe and reliable trading platform. BITGet offers a variety of trading pairs, including BTC/USDT, ETH/USDT and XRP/USDT. Additionally, the exchange has a reputation for security and liquidity and offers a variety of features such as premium order types, leveraged trading and 24/7 customer support.

Ouyi okx installation package is directly included Ouyi okx installation package is directly included Feb 21, 2025 pm 08:00 PM

Ouyi OKX, the world's leading digital asset exchange, has now launched an official installation package to provide a safe and convenient trading experience. The OKX installation package of Ouyi does not need to be accessed through a browser. It can directly install independent applications on the device, creating a stable and efficient trading platform for users. The installation process is simple and easy to understand. Users only need to download the latest version of the installation package and follow the prompts to complete the installation step by step.

Get the gate.io installation package for free Get the gate.io installation package for free Feb 21, 2025 pm 08:21 PM

Gate.io is a popular cryptocurrency exchange that users can use by downloading its installation package and installing it on their devices. The steps to obtain the installation package are as follows: Visit the official website of Gate.io, click "Download", select the corresponding operating system (Windows, Mac or Linux), and download the installation package to your computer. It is recommended to temporarily disable antivirus software or firewall during installation to ensure smooth installation. After completion, the user needs to create a Gate.io account to start using it.

Ouyi Exchange Download Official Portal Ouyi Exchange Download Official Portal Feb 21, 2025 pm 07:51 PM

Ouyi, also known as OKX, is a world-leading cryptocurrency trading platform. The article provides a download portal for Ouyi's official installation package, which facilitates users to install Ouyi client on different devices. This installation package supports Windows, Mac, Android and iOS systems. Users can choose the corresponding version to download according to their device type. After the installation is completed, users can register or log in to the Ouyi account, start trading cryptocurrencies and enjoy other services provided by the platform.

gate.io official website registration installation package link gate.io official website registration installation package link Feb 21, 2025 pm 08:15 PM

Gate.io is a highly acclaimed cryptocurrency trading platform known for its extensive token selection, low transaction fees and a user-friendly interface. With its advanced security features and excellent customer service, Gate.io provides traders with a reliable and convenient cryptocurrency trading environment. If you want to join Gate.io, please click the link provided to download the official registration installation package to start your cryptocurrency trading journey.

How to Install phpMyAdmin with Nginx on Ubuntu? How to Install phpMyAdmin with Nginx on Ubuntu? Feb 07, 2025 am 11:12 AM

This tutorial guides you through installing and configuring Nginx and phpMyAdmin on an Ubuntu system, potentially alongside an existing Apache server. We'll cover setting up Nginx, resolving potential port conflicts with Apache, installing MariaDB (

See all articles