5.Linux scheduled task scheduling crontab

1. Scheduled task scheduling crontab

1. Introduction to scheduled task scheduling crond

①Task scheduling: The system executes specific commands and programs at a certain time

②Task scheduling classification: system work (virus scanning), user work (backup mysql database)

2. Scheduled task scheduling crond schematic diagram

 

3. Syntax of task scheduling

crontab[ options]

4. Common options

-e edit scheduled tasks

-l displays scheduled tasks

-r deletes all tasks for the current user

5.Quick Start Case

Mission requirements

Query the detailed directory under /etc every 1 minute and append the results to /tmp/to.txt

Steps to achieve

①Edit scheduled tasks crontab -e

 There is a space between * , wq save and exit

6. The placeholder indicates  the time, day, month, and week.

7.Explanation of special symbols

 8.Case _

 

2. Case

1. Every 1 minute, append the current date information to the /tmp/mydate.txt file

①Write the script file vim /home/mytask1.sh

date >> /tmp/mydate.txt

②Give the script file executable permissions

rwx=7

r=4

chmod 744 /home/mytask1.sh

③Edit scheduled tasks crontab -e

*/1 * * * * /home/mytask1.sh

2. Every 1 minute, append the current date and calendar information to the /tmp/mydate.txt file

①Write the script file vim /home/mytask2.sh

date >> /tmp/mycal.txt

cal >> /tmp/mycal.txt

②Give the script file executable permissions

rwx=7

r=4

chmod 744 /home/mytask2.sh

③Edit scheduled tasks crontab -e

*/1 * * * * /home/mytask2.sh

3. Back up the mysql database testdb to the file mydb.bak at 2:00 a.m. every day.

①Write the script file vim /home/mytask3.sh

/usr/local/mysql/bin/mysqldump -u root -p root testdb > /tmp/mydb.bak

②Give the script file executable permissions

rwx=7

r=4

chmod 744 /home/mytask3.sh

③Edit scheduled tasks crontab -e

0 2 * * * /home/mytask3.sh

crond related instructions:

1) conrtab -r: Terminate task scheduling.

2) crontab –l: List the currently scheduled tasks

3) service crond restart [restart task scheduling]

Guess you like

Origin blog.csdn.net/jbkjhji/article/details/132875891