current position:Home>Linux slang explanation: what is a scheduled task
Linux slang explanation: what is a scheduled task
2022-01-27 01:34:08 【Linux China】
In this issue 《Linux Slang explanation 》 In the series , You will learn Linux Medium Timing task 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 :
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 that23
The end of the hour is midnight11:59
, Then there is the beginning of each day0
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 from0
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 :
#!/usr/bin/bash
echo "Backing up..."
mkdir -p ~/.local/tmp/
tar -Pc ~/Documents/ -f ~/.local/tmp/backup.gz
The script does the following :
- Make sure the backup path Directory
~/.local/tmp/
There is . - 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 .
Postscript
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 .
via:
author : Topic selection : translator : proofreading :
This paper is written by Original compilation , Honor roll out

copyright notice
author[Linux China],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270134033514.html
The sidebar is recommended
- Spring IOC container loading process
- [thinking] the difference between singleton mode and static method - object-oriented programming
- Hadoop environment setup (MySQL environment configuration)
- 10 minutes, using node JS creates a real-time early warning system for bad weather!
- Git tool
- Force deduction algorithm - 92 Reverse linked list II
- What is the sub problem of dynamic programming?
- C / C + +: static keyword summary
- Idea does not have the artifacts option when configuring Tomcat
- Anaconda can't open it
guess what you like
-
I don't know how to start this
-
Matlab simulation of transportation optimization algorithm based on PSO
-
MySQL slow log optimization
-
[Vue] as the window is stretched (larger, smaller, wider and higher), the text will not be displayed
-
Popular Linux distributions for embedded computing
-
Suzhou computer research
-
After installing SSL Certificate in Windows + tomcat, the domain name request is not successful. Please answer!!
-
Implementation time output and greetings of jQuery instance
-
The 72 year old uncle became popular. Wu Jing and Guo fan made his story into a film, which made countless dreamers blush
-
How to save computer research
Random recommended
- Springboot implements excel import and export, which is easy to use, and poi can be thrown away
- The final examination subjects of a class are mathematical programming, and the scores are sorted and output from high to low
- Two pronged approach, Tsinghua Professor Pro code JDK and hotspot source code notes, one-time learning to understand
- C + + recursive knapsack problem
- The use of GIT and GitHub and the latest git tutorial are easy to understand -- Video notes of crazy God speaking
- PostgreSQL statement query
- Ignition database test
- Context didn't understand why he got a high salary?, Nginxfair principle
- Bootstrap switch switch control user's guide, springcloud actual combat video
- A list that contains only strings. What other search methods can be used except sequential search
- [matlab path planning] multi ant colony algorithm grid map path planning [including GUI source code 650]
- [matlab path planning] improved genetic algorithm grid map path planning [including source code phase 525]
- Iinternet network path management system
- Appium settings app is not running after 5000ms
- Reactnative foundation - 07 (background image, status bar, statusbar)
- Reactnative foundation - 04 (custom rpx)
- If you want an embedded database (H2, hsql or Derby), please put it on the classpath
- When using stm32g070 Hal library, if you want to write to flash, you must perform an erase. If you don't let it, you can't write continuously.
- Linux checks where the software is installed and what files are installed
- SQL statement fuzzy query and time interval filtering
- 69. Sqrt (x) (c + + problem solving version with vs runnable source program)
- Fresh students are about to graduate. Do you choose Java development or big data?
- Java project: OA management system (java + SSM + bootstrap + MySQL + JSP)
- Titanic passenger survival prediction
- Vectorization of deep learning formula
- Configuration and use of private image warehouse of microservice architect docker
- Relearn JavaScript events
- For someone, delete return 1 and return 0
- How does Java dynamically obtain what type of data is passed? It is used to judge whether the data is the same, dynamic data type
- How does the database cow optimize SQL?
- [data structure] chain structure of binary tree (pre order traversal) (middle order traversal) (post order traversal) (sequence traversal)
- Webpack packaging optimization solution
- 5. Operation element
- Detailed explanation of red and black trees
- redhat7. 9 install database 19C
- Blue Bridge Cup notes: (the given elements are not repeated) complete arrangement (arrangement cannot be repeated, arrangement can be repeated)
- Detailed explanation of springboot default package scanning mechanism and @ componentscan specified scanning path
- How to solve the run-time exception of test times
- Detailed explanation of k8s management tool kubectl
- Android system view memory command