search
HomeSystem TutorialLINUXLearn how to configure scheduled tasks in Linux: using cron and anacron
Learn how to configure scheduled tasks in Linux: using cron and anacronJan 15, 2024 am 11:33 AM
linuxlinux tutorialRed Hatlinux systemlinux commandlinux certificationred hat linuxlinux 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

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

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.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
This article is reproduced at:Linux就该这么学. If there is any infringement, please contact admin@php.cn delete
什么是linux设备节点什么是linux设备节点Apr 18, 2022 pm 08:10 PM

linux设备节点是应用程序和设备驱动程序沟通的一个桥梁;设备节点被创建在“/dev”,是连接内核与用户层的枢纽,相当于硬盘的inode一样的东西,记录了硬件设备的位置和信息。设备节点使用户可以与内核进行硬件的沟通,读写设备以及其他的操作。

Linux中open和fopen的区别有哪些Linux中open和fopen的区别有哪些Apr 29, 2022 pm 06:57 PM

区别:1、open是UNIX系统调用函数,而fopen是ANSIC标准中的C语言库函数;2、open的移植性没fopen好;3、fopen只能操纵普通正规文件,而open可以操作普通文件、网络套接字等;4、open无缓冲,fopen有缓冲。

linux中什么叫端口映射linux中什么叫端口映射May 09, 2022 pm 01:49 PM

端口映射又称端口转发,是指将外部主机的IP地址的端口映射到Intranet中的一台计算机,当用户访问外网IP的这个端口时,服务器自动将请求映射到对应局域网内部的机器上;可以通过使用动态或固定的公共网络IP路由ADSL宽带路由器来实现。

什么是linux交叉编译什么是linux交叉编译Apr 29, 2022 pm 06:47 PM

在linux中,交叉编译是指在一个平台上生成另一个平台上的可执行代码,即编译源代码的平台和执行源代码编译后程序的平台是两个不同的平台。使用交叉编译的原因:1、目标系统没有能力在其上进行本地编译;2、有能力进行源代码编译的平台与目标平台不同。

linux中eof是什么linux中eof是什么May 07, 2022 pm 04:26 PM

在linux中,eof是自定义终止符,是“END Of File”的缩写;因为是自定义的终止符,所以eof就不是固定的,可以随意的设置别名,linux中按“ctrl+d”就代表eof,eof一般会配合cat命令用于多行文本输出,指文件末尾。

linux怎么查询mac地址linux怎么查询mac地址Apr 24, 2022 pm 08:01 PM

linux查询mac地址的方法:1、打开系统,在桌面中点击鼠标右键,选择“打开终端”;2、在终端中,执行“ifconfig”命令,查看输出结果,在输出信息第四行中紧跟“ether”单词后的字符串就是mac地址。

linux怎么判断pcre是否安装linux怎么判断pcre是否安装May 09, 2022 pm 04:14 PM

在linux中,可以利用“rpm -qa pcre”命令判断pcre是否安装;rpm命令专门用于管理各项套件,使用该命令后,若结果中出现pcre的版本信息,则表示pcre已经安装,若没有出现版本信息,则表示没有安装pcre。

linux中rpc是什么意思linux中rpc是什么意思May 07, 2022 pm 04:48 PM

在linux中,rpc是远程过程调用的意思,是Reomote Procedure Call的缩写,特指一种隐藏了过程调用时实际通信细节的IPC方法;linux中通过RPC可以充分利用非共享内存的多处理器环境,提高系统资源的利用率。

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!