Linux scheduled tasks Crontab details of recommendations

  • 2020-05-14 05:54:12
  • OfStack

Today I made a database backup script, by the way the system has to learn 1 Linux timing execution script Settings. The timing execution under Linux is mainly performed by adding the custom plan into the crontab file. The setup is a little more complicated than Windows (because there is no graphical interface), but it is not very complicated. Basically, you can remember the directory /var/spool/cron after using it once, but the key is to remember the directory /var/spool/cron. Here's how to use it:

First look at the /etc/crontab file:


$ cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

The first four lines are about setting the environment variables for the cron task to run. The value of the SHELL variable specifies the SHELL environment used by the system (bash shell in this example), and the PATH variable defines the path to execute the command. The output of Cron is emailed to the user name defined by the MAILTO variable. If the MAILTO variable is defined as an empty string (MAILTO=""), the E-mail will not be sent. The HOME variable can be used to set the base directory when executing a command or script.

The format of the description of each line of tasks in file /etc/crontab is as follows:


 minute hour day month dayofweek command
minute - integers from 0 to 59 hour - integers from 0 to 23 day - integers from 1 to 31 (must be the valid date of the specified month) month - integers from 1 to 12 (or months as Jan or Feb) dayofweek - integer from 0 to 7, 0 or 7 is used to describe Sunday (or Sun or Mon) command - commands to be executed (as ls /proc can be used) > > /tmp/proc or commands to execute custom scripts) root means to run as an root user run-parts means that a folder is followed by all the scripts under that folder

For each of the above statements, the asterisk (*) represents all available values. For example, * when referring to month means monthly execution of the command (subject to other restrictions).

A hyphen (-) between integers indicates an integer column, for example, 1-4 means the integers 1,2,3,4

The specified value is separated by commas. For example: 3,4,6,8 represents the four specified integers.

The symbol "/" specifies the step setting. "/" represents the step value. For example, 0-59/2 is defined to execute once every two minutes. The step value can also be represented by an asterisk. For example, */3 is used to run the specified tasks every 3 months.

Comment lines beginning with "#" will not be executed.

If an cron task needs to be performed on a regular basis rather than by hour, day, week, or month, you need to add the /etc/ cron.d directory. All files in this directory have the same syntax as files /etc/crontab, see the sample:


# record the memory usage of the system every monday 
# at 3:30AM in the file /tmp/meminfo 
30 3 * * mon cat /proc/meminfo >> /tmp/meminfo 
# run custom scr and pt the first day of every month at 4:10AM 
10 4 1 * * /root/scr and pts/backup.sh

Users other than root users can perform crontab configuration plan tasks. All user-defined crontab are stored under directory /var/spool/cron, and the task is performed as the creator. To create an crontab with a specific user, log in as that user, execute the command crontab-e, and the system will start editing crontab with the editing software specified in VISUAL or EDITOR. The file content is the same as /etc/crontab. Here's an example:


0 3 * * * /home/dbbackup/db1backup.sh backup
0 4 * * * /home/dbbackup/db2backup.sh backup

Said every day at 3 o 'clock to perform/home dbbackup/db1backup sh backup, 4 PM execution/home dbbackup/db2backup sh backup, if it is performed once every 5 minutes can be changed to:


*/5 * * * * /home/dbbackup/db2backup.sh backup

When change crontab need to save, files are saved in/into the following file var/spool/cron/username. The file name will vary according to the user name.

cron service will check 1 time/per minute etc crontab, / etc/cron d /, / var spool/cron file changes. If a change is found, it is downloaded to memory. Therefore, even if the crontab file changes, the program does not need to be restarted. Recommend custom task using crontab - e commands to add, after using/etc/init d/crond restart command restart crond process, official documents that don't have to restart the process, but I have not restart unable to run the task. At first I didn't know what run-parts in the /etc/crontab file meant. I simply added the command in the /etc/crontab format and it always failed. Later I learned that run-parts refers to the folder following it.

The following is an introduction:

************************************************************************************

cron is an linux timing execution tool that runs jobs without human intervention. Since Cron is a built-in service of Linux, but it does not automatically get up, you can start and stop the service by:


 /sbin/service crond start // Start the service 
/sbin/service crond stop // Close the service 
/sbin/service crond restart // Restart the service 
/sbin/service crond reload // Reload configuration 

You can also start the service automatically when the system starts:

In/etc rc. d/rc. local plus at the end of the script:


/sbin/service crond start

Now that the Cron service is in the process, we can use it. The Cron service provides the following interfaces for you to use:

1. Edit directly using the crontab command

The cron service provides the crontab command to set the cron service. The following are some parameters and instructions of this command:


 crontab -u // Set a user's cron Service, 1 a root The user needs this parameter when executing the command 
crontab -l // List a user cron Details of the service 
crontab -r // Delete not a single user cron service 
crontab -e // Edit a user's cron service 

For example, root check its cron Settings: crontab-u root-l

For example, root wants to delete cron Settings for fred: crontab-u fred-r

When editing the cron service, there are some formats and conventions for editing the content. Enter crontab-u root-e

Enter the vi edit mode, and edit content 1 must conform to the following format: */1 * * * * ls > > /tmp/ls.txt

The first part of this format is the setting of time, the second part is the command to be executed, if there are too many commands to be executed, you can write these commands into a script, and then directly call the script here, remember to write out the full path of the command when calling. We have a definite agreement on the setting of time. The five * signs in front represent five Numbers. The value range and meaning of the Numbers are as follows:

59 minutes (0 -) Small � (0-23) Date (1-31) (1-12) Week (0-6) //0 represents Sunday

In addition to Numbers, there are a few special symbols: "*"," /", and "-","* "for all Numbers in the range of values," /" for each,"*/5" for every five units," -" for going from a number to a number,"," separate discrete Numbers." Here are some examples to illustrate the problem:

Every morning at 6 o 'clock


0 6 * * * echo "Good morning." >> /tmp/test.txt // Pay attention to the simple echo You can't see any output from the screen because cron Take any output email to root The mailbox. 

Every two hours


0 */2 * * * echo "Have a break now." >> /tmp/test.txt

Every two hours between 11 p.m. and 8 a.m., 8 a.m


 minute hour day month dayofweek command
0

Every month on the 4th and every week from 1 a.m. to 3 a.m


 minute hour day month dayofweek command
1

January 1 at 4 a.m


 minute hour day month dayofweek command
2

Every time after editing a user's cron Settings, cron automatically generates a file with the same name as the user under /var/spool/cron. The user's cron information is recorded in this file. This file cannot be edited directly, but can only be edited with crontab-e. After cron starts, read the file once every time and check to see if you want to execute the commands inside. Therefore, there is no need to restart the cron service after this file is modified.

2. Edit /etc/crontab file to configure cron

The cron service not only reads all the files in var/spool/cron once per minute, but also reads all the files in etc/crontab once per minute, so we can configure this file to do something with the cron service as well. The crontab configuration is for a user, while editing /etc/crontab is for a system task. The file format of this file is:


SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root // If an error occurs, or there is data output, the data is sent to the account as an email 
HOME=/   // The path the consumer runs , Here is the root directory 
# run-parts
01 * * * * root run-parts /etc/cron.hourly // Hourly execution /etc/cron.hourly In the script 
02 4 * * * root run-parts /etc/cron.daily // Every day /etc/cron.daily In the script 
22 4 * * 0 root run-parts /etc/cron.weekly // Weekly execution /etc/cron.weekly In the script 
42 4 1 * * root run-parts /etc/cron.monthly // Monthly execution /etc/cron.monthly In the script 

Note that "run-parts" is a parameter. If you remove this parameter, you can write the name of the script you want to run, not the folder name.


Related articles: