Learn how to configure scheduled tasks in Linux: using cron and anacron
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. |
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
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 Linuxanacron 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
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
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
当机器在 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
下面是 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.html Editor: 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version
