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 set up scheduled tasks in Linux system

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

Share

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

This article mainly introduces how to set planning tasks in the Linux system, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Cron is set in Linux

Cron is a daemon that runs scheduled tasks such as system backups, updates, and so on. It is suitable for scheduled tasks that run on machines where 24X7 is running continuously, such as servers.

The command / script is written in the cron task script, which is scheduled in the crontab file. The default cromtab file for the system 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, type:

$crontab-e

How to set up anacron in Linux

Anacron is used to run commands on a daily basis. It works slightly differently from cron in that it assumes that the machine won't turn on all the time.

Cron is also suitable for running daily, weekly, and monthly scheduled tasks on machines that do not 24X7, such as laptops and desktops. LCTT is not suitable for hourly and minute tasks.

Suppose you have a scheduled task (such as a backup script) to run every day in the middle of the night using cron, and maybe you fall asleep when your desktop / laptop is turned off. Your backup script will not be run.

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

How anacron works at Linux

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

Period delay job-identifier command from the above format:

Period-this is the frequency of the task, specified in days, or @ daily, @ weekly, @ monthly represent daily, weekly, and 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 executing 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 be executed.

To browse the sample file, enter:

$ls-1 / 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 actually happening:

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 delay field is executed after waiting for the number of minutes specified in the command field.

Once the task is executed, it records the date in the 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 runs the / home/aaronkilik/bin/backup.sh script every day:

@ daily 10 example.daily / bin/bash / home/aaronkilik/bin/backup.sh when the machine is turned off when backup.sh is expected to run, anacron will run it ten minutes after the machine is powered on, instead of waiting for 7 days.

Here are two important variables of the anacrontab file that you should understand:

START_HOURS_RANGE-this sets the time range in which the task starts to run (that is, the task only runs within a few hours).

RANDOM_DELAY-this defines the maximum random delay 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/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin HOME=/root LOGNAME=root # These replace cron's entries 15 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 below is a comparison of cron and anacron To help you understand when to use one of them.

Cronanacron, it's a daemon, it's not a daemon, it's suitable for servers, it's suitable for desktops / laptops, it allows you to run scheduled tasks at a minute level, it only allows you to run scheduled tasks on a day-to-day basis, and scheduled tasks don't execute when scheduled tasks are shut down. If scheduled tasks expire, the machine is powered off. Then it will perform scheduled tasks after the next boot of the machine. Only root users and root users can use it. The main difference between cron and anacron is that cron can run effectively on machines that continue to run, while anacron is for machines that will shut down within a day or a week.

Thank you for reading this article carefully. I hope the article "how to set up Planning tasks in Linux system" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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