Mysql > update mysql > update mysql > update mysql > update mysql > update mysql > update mysql

1. Install the CronTabs service and enable automatic startup upon startup

Yum install crontabs systemctl enable crond systemctl start crond systemctl status crondCopy the code

2. Set a user-defined scheduled task

vi /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
#| .------------- hour (0 - 23)
#| | .---------- day of month (1 - 31)
#| | | .------- month (1 - 12) OR jan,feb,mar,apr ...
#| | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
#| | | | |
# *  *  *  *  * user-name  command to be executed
*/1 * * * * root /home/test.sh
#Full mysql backup is performed at 23:30 every day
30 23 * * * root /home/dbback/dbbackall.sh
#The backup of the history table is performed at 12:30 am and 12:30 am
30 0,12 * * * root /home/dbback/dbback.sh
Copy the code

Minute (0-59) Hour (0-23) Day (1-31) month (11-12) Week (0-6,0 indicates Sunday) Command to be executed

  • */30 * * * root /usr/local/mycommand.sh(Run myCommand every 30 minutes every day)
  • * 3 * * * root /usr/local/mycommand.sh(Every day at 3am, execute command script, PS: because the first minute is not set, then execute command every minute at 3am every day.)
  • 0 3 * * * root /usr/local/mycommand.sh(Thus executing the command script every day at 3am sharp)
  • */10 11-13 * * * root /usr/local/mycommand.sh(It is also common to run command scripts every 10 minutes between 11:00 and 13:00 every day.)
  • 10-30 * * * * root /usr/local/mycommand.sh(10 to 30 minutes per hour, execute the command script once per minute, 20 times in total)
  • 10,30 * * * * * root /usr/local/mycommand(Run the command script twice every 10 and 30 minutes every hour.)

3. Save the Settings and take effect

Load a task to make it take effect: crontab /etc/crontab

Run the following command to view tasks: crontab -l $crontab -u User name -l (List the scheduled tasks of the user)

PS: Pay special attention to, the crond task plan, will not call users to set the environment variable, it has its own environment variables, when you use some commands, such as mysqldump command need environment variable, when executing the script manually is normal, but when you use the crond execution will not, then either you write the full absolute path, Or add the environment variable to /etc/crontab.

Well, that’s how simple it is to plan a task, but if you plan a task to execute multiple statements, you need to use a shell script, write a shell script yourself, and then execute the script in the scheduled task. As for shell script writing, I won’t go into details here.