How to set up Scheduled tasks in Linux: Cron and Anacron

Source: Internet
Author: User

Guide In this article, we'll explain Cron and anacron, and show you how to set up Anacron in Linux. We will also compare these two tools.

650) this.width=650; "class=" Alignnone size-full wp-image-75111 "src=" http://www.linuxprobe.com/wp-content/uploads/ 2017/06/33-1.jpg "width=" "height=" "style=" Height:auto;/>

Cron in Linux Setup

Cron-is a daemon that is used to run scheduled tasks such as system backups, updates, and so on. It is suitable for scheduled tasks that run on 24x7 machines such as servers.

The command/script is written in the Cron task script, which is scheduled in the crontab file. The system default cromtab file is /etc/crontab , but each user can also create their own cromtab file to run user-defined commands at a specific time.

To create a personal crontab file, simply enter:

$ crontab-e

650) this.width=650; "class=" Alignnone wp-image-75107 size-full "src=" http://www.linuxprobe.com/wp-content/uploads/ 2017/06/11-3.jpg "width=" 925 "height=" 406 "style=" Height:auto; "/>

How to set up Anacron in Linux

The Anacron is used to run commands in days of frequency. Its work is slightly different from Cron, which assumes that the machine does not always boot .

Cron is also ideal for running daily, weekly, and monthly scheduled tasks on machines that do not run 24x7, such as laptops and desktops (LCTT: Do not perform tasks by hours or minutes).

Suppose you have a scheduled task (such as a backup script) to use Cron to run at midnight every day, and maybe you as well as sleep when your desktop/laptop is turned off. Your backup script will not be run.

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

Anacron how to work on Linux

The Anacron task is listed in /etc/anacrontab , and the task can be arranged using the following format (the comments in the Anacron file must start with the # number).

Period Delay Job-identifier Command

From the above format:

  • period-This is the frequency of the task, specified in days, or is@daily、@weekly、@monthlyRepresents daily, weekly, monthly. You can also use numbers:1Every day7Weekly --Monthly, orNDays

  • Delay -This is the number of minutes to wait before performing a task.

  • Job-id -This is the unique name of the task written in the log file.

  • Command-This is the order or shell script to execute.

To browse the sample file, enter:

$ ls-l/var/spool/anacron/total 12-rw-------1 root root 9 June 1 10:25 cron.daily-rw-------1 root root 9 May 11:01 C RON.MONTHLY-RW-------1 root root 9 May 10:28 cron.weekly

This is actually happening:

    • Anacron checks if the task is already in  period   field is executed. If not, wait for   delay After the number of minutes specified in the   field, execute  command the command specified in the field.

    • Once the task is executed, it will use  job-id the name specified in the   (timestamp file name) field to record the date in  /var/spool/anacron in the timestamp file in the   directory.

Now let's look at an example. This will run the /home/aaronkilik/bin/backup.sh script every day:

@daily Ten example.daily/bin/bash/home/aaronkilik/bin/backup.sh

When the machine is shut down when the backup.sh is expected to run, Anacron will run it 10 minutes after the machine is booted without having to wait 7 days.

Here are two important variables you should understand for the Anacrontab file:

    • Start_hours_range -This sets the time range in which the task starts running (that is, the task runs only within these hours).

    • Random_delay -This defines the maximum random delay that is added to the user-defined task delay (default is 45).

This is what your Anacrontab file might look like.

anacron–  /etc/ Anacrontab  :

# /etc/anacrontab: configuration file for anacron# see anacron (8)  and  anacrontab (5)  for details. shell=/bin/shpath=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binhome=/rootlogname=root#  These replace cron ' s entries1       5        cron.daily      run-parts --report /etc/ cron.daily7       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

Here's a comparison of Cron and Anacron to help you understand when to use one of them.

Cron Anacron
It's not a daemon
fit server for desktop/laptop
can let you run scheduled tasks in minutes You can only run scheduled tasks on a daily basis
No scheduled tasks are performed when shutting down
normal user and Root Users can use the

The main difference between Cron and Anacron is that Cron can run efficiently on those machines that are constantly running, while Anacron is for machines that will shut down in a day or week.

If you also know other ways, please share them with us in the comments section.


Original address: http://www.linuxprobe.com/cron-anacron-work. html

This article is from the "blog" blog, please be sure to keep this source http://coderhsf.blog.51cto.com/12629645/1934758

How to set up Scheduled tasks in Linux: Cron and Anacron

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.