Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use cron and anacron

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you how to use cron and anacron, I believe most people still do not know how to use, so share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

cron is a service that runs routine tasks in linux, and the daemon is crond, a daemon that runs scheduled tasks such as system backups, updates, etc. It is suitable for scheduled tasks running on machines such as servers that are running 7×24 hours a day. If your server is not running 7×24 hours a day, the scheduled task execution time may be just during the server shutdown period, which can be used by anacron.

cron in Linux settings

cron-is a daemon used to run scheduled tasks such as system backups, updates, etc. It is suitable for scheduled tasks running on machines that run 24 x 7 non-stop, such as servers.

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

To create a personal crontab file, simply enter:

$ crontab -How to set scheduled tasks in eLinux: cron and anacron How to set scheduled tasks in Linux: cron and anacron How to set anacron in Linux

Anacron is used to run commands with a frequency of days. It works slightly differently from cron, assuming that the machine won't always be on.

cron is also good for running daily, weekly, and monthly scheduled tasks on machines that don't run 24 x 7, such as laptops and desktop computers.

Suppose you have a scheduled task (such as a backup script) to run every night using cron. Maybe you're asleep 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.

How anacron works on Linux

Anacron tasks are listed in/etc/anacron tab and can be arranged using the following format (comments in anacron files must start with a #):

period delay job-identifier command

From the format above:

period-This is the frequency of the task, specified in days, or @daily,@weekly,@monthly for daily, weekly, monthly. You can also use numbers: 1-daily, 7-weekly, 30-monthly, or N-days. 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 command or shell script to execute.

To browse the sample file, type:

$ 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

Here's what actually happened:

Anacron checks to see if the task has been executed at the time specified in the period field. If not, the command specified in the command field is executed after waiting for the number of minutes specified in the delay field. Once the task is executed, it records the date in a timestamp file in the/var/spool/anacron directory using the name specified in the job-id (timestamp file name) field.

Now let's look at an example. This script runs daily at/home/aaronkilik/bin/www.example.com: backup.sh

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

When the machine is turned off when it is expected to be run at backup.sh, anacron will run it ten minutes after the machine is turned on, instead of waiting seven days.

Here are two important variables in anacontab files that you should understand:

START_HOURS_RANGE-This sets the time range in which the task starts running (i.e. the task runs only during these hours). RANDOM_DELAY-This defines the maximum random delay added to the user-defined task delay (default is 45).

Here's what your anacontab 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.

cronanacron It is a daemonIt is not a daemonSuitable for servers Suitable for desktops/laptops Allows you to run scheduled tasks on a minute basis Only allows you to run scheduled tasks on a day basis Scheduled tasks will not be executed when the machine is shutdown If scheduled tasks expire and the machine is shutdown, then it will execute scheduled tasks when the machine is next powered onAvailable to both normal and root users Only root can use (start normal tasks with specific configuration)

The main difference between cron and anacron is that cron can run efficiently on machines that run continuously, while anacron is for machines that shut down within a day or a week.

The above is "cron and anacron how to use" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report