crontab command
The crontab command is a tool used in Linux to set up repeated execution of commands or scripts. It is able to execute commands or scripts at a certain time interval for a specified period of time, as required.
Basic usage of crontab
crontab [-u <user>] [-e|-l|-r]
crontab <filename>
crontab <filename>
You can read a file written in crontab syntax and perform a timed task according to the instructions in the file. At the same time, it is crontab -l
possible to list all scheduled tasks for the current user on standard output. Therefore, we can use crontab -l > <filename>
to save the current crontab situation, when necessary (such as mistakenly delete) with crontab <filename>
read into the recovery.
crontab -e
The system default editor is started. This editor is specified by the system environment variable EDITOR, for example export EDITOR=vi
, the crontab -e
VI is started to edit the crontab timed task file for the current user. crontab -r
all crontab Scheduled tasks for the user are deleted.
The option to specify the user -u <user>
will change the behavior of the -e/-l/-r
options. If you do not -u
specify a user, the default action is for the current user or, conversely, for the specified user. For example crontab -u Liam -r
, you would delete the Crontab timed task of the user Liam--of course, you need to have sufficient permissions to do so.
Format of crontab files
Whether you are crontab <filename
reading a timed task with > from a file, or crontab -e
editing a timed task, we will be in touch with the crontab format command.
The crontab command format is as follows:
From the above we can see:
- A crontab command is divided into six columns, written on a single line;
- The first 5 columns are used for timing, indicating when the execution begins;
- The 6th column specifies the command or script that needs to be executed regularly;
In addition, as with Shell scripts, we can use # to represent annotations in crontab files.
Fancy timing
In the first 5 columns used for timing in the crontab command, the following symbols are supported for "Fancy timing":
- Star Flower (*): represents all possible values;
- Commas (,): Separating the different ranges of the same field with commas;
- Hyphen (-): Connects two integers with hyphens, representing an integer range;
- Slash (/): is actually a division slash, which is used to specify the time interval frequency.
Some examples
With this knowledge, we can look at some examples.
# Execute once per minute * * *
command
# Execute every 5 minutes */5 * * * *
command
# 15 minutes and 45 minutes per hour
15,45 * * * * * * * COMM and # Night 20:00-20:15 0-15 * * * * * * * *
command
# every Monday 10 o'clock in the morning
0 * * 1 command
find--, get some fun.
The general form of the find command given by man is:
Find [-h] [l] [P] [D-debugopts] [-olevel] path ... [Expression]
Few of them [-H] [-L] [-P] [-D debugopts] [-Olevel]
will be used. Therefore, the general form of the find command can be reduced to f ind path ... [expression];
, where the appropriate file is found in some paths (the default recursively include Subpath), and then the corresponding action is performed according to expression.
path ...: find
Path for command lookup
Expression: the concrete form is-parameters [-exec -ok]
-parameters
There are a lot of the next section to describe the commonly used parameters
-exec command {}
\; Execute a Shell command
-ok command {} \
; with-exec, but before executing each command, prompts the user to confirm
Commonly used parameters
-name
: Find files by file name, accept wildcard characters
Find. -name "*.cpp": recursively find all files with the suffix. cpp in the current directory (.) and subdirectories, and print in standard output
-perm
: Find files by file permissions
Find. -perm 755: Recursively Find all files (directories) with permission 755 in the current directory (.) and subdirectories and print them in standard output
Find. -perm 644-name "*.cpp": recursively find all. cpp files with permission 644 in the current directory and subdirectories, and print them in standard output
-group
: Search by file's owning group
-user
: Find by Owner of file
Find. -user search: In the current directory (.) and subdirectories to recursively find all search accounts belong to the file, and print in the standard output
-atime, -mtime, -amin, -mmin
: "A representation that starts with the last access time to look up the",m "to find";-n in the last modified time indicates n time, +n represents n time before
Find. -atime + 7: Recursively find all files that were last accessed before 7 days in the current directory (.) and subdirectories and print them in standard output
Find. -MMIN-10: Recursively find all files that were last modified within 10 minutes in the current directory (.) and subdirectories and print them in standard output
-newer file1 ! file2
: Find files that are newer than file1 but older than file2
-type [b|d|c|p|l|f]
: Find by File type
B: Block device files
D: Table of Contents
C: Character device files
P: Pipe File
L: Symbolic Link file
F: Ordinary Documents
-follow
: If the find command encounters a symbolic link file, it tracks to the file that the link points to
-delete
: Deletes a found file or directory
Regular cleanup
At this point, it becomes easy to clean up useless files regularly. We only need to combine the Find command option in the Crontab -delete
.
0 8 * * * find/home/s/coredump-user search-type f-mtime +7-delete
Here, we execute the find command at 8 o'clock in the morning every day, and the command will search the/home/s/coredump directory for an ordinary 7-day file created by the search user and delete it.
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.