Linux slang explanation: what is a scheduled task

Linux China 2022-01-27 01:34:08 阅读数:283

linux slang explanation scheduled task

In this issue 《Linux Slang explanation 》 In the series , You will learn Linux Medium Timing task cron job function . You will learn to edit  crontab  File to create a scheduled task .

What is a scheduled task

cron  Is a practical command line tool for running short and fast commands on schedule . The tool is a convenient 、 Classic system management tools , By combining with other tools, you can automatically run a variety of tasks . such as , Some people put  rsync  and  cron  Use a combination of , Automatically create daily and weekly backups at specific times . Some people also use  cron  To analyze server logs , And combined with the function of e-mail system , Automatically send an alarm message when an error is detected in the log .

cron  Just as “ Swiss Army knife ” equally , It can be used in multiple scenarios . Use your imagination as much as you can , To explore its function .

Actually  cron  It's easy to use , It only takes a few seconds . But before we start , Let's start with a few concepts that are often confused .

cron、 Timing task 、crontab

There are three terms that are easy to confuse :cron、 Timed tasks and  crontab, Let's see what they mean :

The term meaning
cron This is the actual execution of scheduled tasks installed on the system  .
Timing task “ Mission ” A program that starts and runs .cron  You can run various tasks according to the agreed time schedule , Such a task is often called “ Timing task ”.
This is a document , Used to define scheduled tasks . One  crontab  Documents can be in tabular form ( Each line is a scheduled task ) Define multiple scheduled tasks .

Let's take a simple example : Create a scheduled task , Every hour to  crontabl_log.txt  Document printing  Linux is cool!.

0 * * * * echo "Linux is Cool!" >> ~/crontab_log.txt

Is it an example of such a simple scheduled task that scares you , This is because you need to know how to read the properties of a scheduled task .

I will talk about this basic theoretical knowledge later .

Get started cron

Let's take another example to see  cron  How to work .

To create a scheduled task ( Or say  cron  Command tasks to be executed ), You just need to run :

crontab -e

This will open a file , Used to edit scheduled tasks :

Crontab default view

among , All with  #  The first lines are all comments , Used to guide you on how to use  cron, If you feel useless, you can delete them .

We will create the following tasks , As our first scheduled task :

* * * * * touch ~/crontab_test

Let me take a quick look at what the task will do :

Scheduled tasks are based on “ minute Hours God month Zhou command ” In form :

  • minute : It refers to the minute when the task will be executed . therefore , The value is  0  It means running at the beginning of each hour ,5  On the... Of each hour 5 Minutes will run .
  • Hours : It refers to the hour of the day when the task will be performed , The value range is  0-23. No,  24  The reason is that  23  The end of the hour is midnight  11:59, Then there is the beginning of each day  0  when . The definition logic of the value range of minutes is similar .
  • God : Refers to the day of the month when the task is performed , The value range is  1-31( Different from the previous minutes and hours from  0  Start taking value ).
  • month : Refers to the month in which the task is performed , The value range is  1-12.
  • Zhou : The day of the week on which the task is performed , From Sunday , The value range is  0-6( Corresponding to Sunday 、 Monday to Saturday ).
  • command : It's the command task you want to run .

If you want to “ minute Hours God month Zhou ” Some have a more detailed understanding , You can refer to  , This website can help you understand what you are doing .

Follow the previous example  * * * * * touch ~/crontab_test, Means to create every minute  ~/crontab_test  file .

Let's edit the task into  crontab  Then look at the results of the execution :

Wait until the next minute , You will find that there are many files in your home directory  crontab_test:

This is  cron  Basic application examples of .

A practical example of timed tasks

Suppose you want to create a script , It is used to copy the contents of multiple directories to one path and package them as backup , How to implement ?

adopt  cron  Scheduled tasks can easily achieve this function .

Please see the following script :

echo "Backing up..."
mkdir -p ~/.local/tmp/
tar -Pc ~/Documents/ -f ~/.local/tmp/backup.gz

The script does the following :

  1. Make sure the backup path Directory  ~/.local/tmp/  There is .
  2. Put the table of contents  ~/Documents/  Package all the contents of the file into a file  ~/.local/tmp/backup.gz.

Let's run the script manually first , See how it works .

First , We are at home (~) Create the script under , The order is  backup_script, As shown in the figure below :

Then edit  backup_script  Script , Write the script code above .

next , give  backup_script  Executable rights :

Finally, run the script  ~/backup_script, Perform functional verification :

You can run the command  tar -xf ~/.local/tmp/backup.gz -C <output_dir>  For backup and recovery , here  <output_dir>  It refers to the path and directory to which the file is to be restored .

Next , You can use it  cron  Tool to perform scheduled tasks and run the script .

for instance , Suppose you need early morning every day 3 Click to run the backup script , You can  crontab  Enter the following command :

* 3 * * * ~/backup_script

So you can automatically backup every day .


This paper briefly introduces the function of timed task . Even though I'm not sure Linux How many desktop users use this feature , But I know that the scheduled task function is widely used by many system administrators . If you have any idea , Feel free to leave a comment in the comments section .


author :  Topic selection :  translator :  proofreading :

This paper is written by    Original compilation ,  Honor roll out

copyright:author[Linux China],Please bring the original link to reprint, thank you.