“This is the 11th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021.”
⭐ Scheduled task scheduling: the system executes specific commands or programs at a certain time. Some important tasks must be performed on a recurring basis, such as virus scanning. Some users want to perform certain programs, such as a scheduled backup of the mysql database.
1. crontab
Scheduled Task Scheduling
-
Syntax: crontab [option]
-
Commonly used options
-e
: Edit crontab scheduled taskcrontab -e
】-l
: Query crontab taskscrontab -l
】-r
Delete all crontab tasks of the current usercrontab -r
】
-
Run the service cronb restart command to restart task scheduling
-
Run the /etc/crontab command to set the task scheduling file
-
Example: */1 * * * * ls -l /etc/> /tmp/to. TXT ==> Run the ls -l /etc/> /tmp/to. TXT command every minute in an hour
-
⭐5 * refer to the minute of an hour, the hour of a day, the day of a month, the month of a year, and the day of a week (0/7 is Sunday).
Try not to use days and days at the same time.
-
A placeholder
*
Legend description:
-
Time item case of scheduled scheduling:
⭐ about */1 * * * * ls -l /etc/> / TMP /to. TXT
⭐** Crontab classic case **
- 1️ every 1 minute, the current date information, addition to
/tmp/mydate
In the file:*/1 * * * * date >> /tmp/mydate
- 2️ every 1 minute, append the current date and calendar to
/home/mycal
In the file:*/1 * * * * date cal >> /home/mycal
- 3️ back up mysql database testdb to a file at 2.00am everyday (backup instruction:
Mysqldump -u root -p password database > /home/db.bak
)crontab -e
0 2 * * * mysqldump -u root -p123456 testdb > /home/db.bak
2. at
Task scheduling
- The at command isA one-time scheduled task— Execute task queue (after executing a task, the task will not be executed again), atDaemon atDWill run in background mode, check the job queue to run.
In default modeatdThe daemon checks the job queue every 60 seconds. When a job exists, the daemon checks the running time of the job. If the time matches the current time, the daemon runs the job.
When using the AT command, make sure that the ATD process is started.ps ef | grep atd
- ⭐at Command format:
At < time > < command >
- ⭐
atq
: View the task queue - ⭐
Atrm < no. >
: Deletes the task whose id is specified - ⭐atTask schedulingThe instance
- It will be executed 2 days later at 5pm
/bin/ls /home
atq
Command to view the system work queue- Output time to the specified file at 17:00 tomorrow for example
/root/date100.log
- After 2 minutes, output time to the specified file such as
/root/date200.log
- Example Delete an existing task.
Atrm < no. >
- It will be executed 2 days later at 5pm
Ctrl + D
End The input of the AT command- 🔥 Ctrl + D: End Of File, EOF or End Of Input
- 🔥 Ctrl + C
We hope this article will help you 🧠 feel free to leave your thoughts in the comments 🌊, we will discuss and share 🔥