Location>code7788 >text

Timed task crond

Popularity:84 ℃/2024-11-19 19:18:04

[1], what is a timed task

1. What is a timed task
Alarm Clock/Daily Timer 7:30 8:00
Do something at a fixed time.
2. Role of timed tasks
Fixed-time time synchronization
Data Backup (Backup Server) 3 copies of important data are retained Company Backup Server Laptop Mobile Hard Disk/Net Disk One copy
Packaging and then backing up (hundreds or thousands of code files) Uses up disk io Reduces transfer speed i input o output
Daily work requirements
Log cutting Prevents logs from becoming too large
/var/log/
mv /var/log/nginx /var/log/
Monitor Fetch + Monitor
Timed script execution
Auxiliary program operation
.....

【2】、System Timing Tasks

Five stars: there must be a space between the stars.
* Minutes 0 - 59 01 or write 1 for 1 minute 0 for full hour
* Hour 0 - 23 00 or write 0 for 12 AM
* Day 1 - 31
* Months 1 - 12 1 - 12 months, can also use English for months jan,feb,mar,apr ...
* Week 0 - 6 0 or 7 for Sunday
Special symbols for time.
* means every If it is five stars, it means that the task will be executed every minute.
/ means interval */5 or */05 means every 5 minutes.
- means interval 7-11 means from 7:00 am to 11:00 am.
, means interval 7-11,13-15 7am to 11am and 1pm to 3pm.


Configuration file: /etc/crontab
Syntax structure: * * * * * * root Executable commands; commands

Example 1. Execute every 5 minutes echo oldboy >> # Default file without path will generate /root/ in home directory.
*/5 * * * * * root echo xu >>

Example 2. Execute a timed task at the 5th minute of every hour.
05 * * * * * root cmd
# Note the difference between 05 and */5
# Note the difference between 05 and */5. Note that the time is calculated in whole hours, not the current time. For example, the next execution time of a timed task created at 03 is 05 1.05 1.10 1.15 1.20.

Example 3. Execute a timed task every minute.
* * * * * * root cmd

Case 4. Execute the task from 7-11 in the morning
* 7-11 * * * * # Indicates that every minute at 7:00 and every minute from 8-11:00 will be executed 7.01 7.02
00 7-11 * * * * # Indicates that it will be executed once at 7, 8, 9, 10, and 11.

Case 5. Execute a command at 12 AM Written Exam Questions
00 00 * * * * root cmd # indicates 12am

Case 6. 7-11,13-15 time interval use
00 7-11,13-15 * * * root cmd

Case 7. Pack /etc/hosts /etc/passwd every minute and put it in the /opt directory.
* * * * * * root tar zcvf /opt/ /etc/hosts /etc/passwd

Case 8. Pack /etc/hosts /etc/passwd every minute with the name of the time + put it in the /opt directory.
# Note that the timed task does not recognize the % need to add \ crowbar or write the command to the script, the timed task to execute scripts
* * * * * * root tar zcvf /opt/`date +\%F-\%H-\%M` /etc/hosts /etc/passwd
[root@kylin-xu opt]# ls

[root@kylin-xu opt]# tar tf
etc/hosts
etc/passwd

# Write the script to execute
[root@kylin-xu opt]# mkdir /server/scripts -p
[root@kylin-xu opt]# cd /server/scripts/
[root@kylin-xu scripts]# vim
cd /etc/
tar zcvf /opt/`date +%F-%H-%M` hosts passwd
[root@kylin-xu scripts]# vim /etc/crontab
* * * * * * root bash /server/scripts/


Case 9. Perform 1 time synchronization every 5 minutes.
*/5 * * * * * root ntpdate

Log file for timed tasks /var/log/cron
# If the timed task is not executed successfully you can find out the problem by checking the logs.

Ubuntu

cron log files
/var/log/syslog

[3], User Timed Tasks

Configuration file.
/var/spool/cron/root # root is the configuration file for the root user to do timed tasks.
There are two ways to configure a user's scheduled tasks.
The first way: with syntax checking.
crontab -e direct return is to edit /var/spool/cron/root visudo -->/etc/sudoers

Second method: Edit the configuration file directly with vim
vim /var/spool/cron/root

Example 1. User timed task creates an echo action to be executed every minute.
# User timed task syntax and system timed task syntax User timed task does not need to add a user.
[root@kylin-xu cron]# crontab -e
* * * * * * * echo test >>

Viewing User Timed Tasks Method 1: [root@kylin-xu] # crontab -e
[root@kylin-xu cron]# cat /var/spool/cron/root
* * * * * * echo test >>
Viewing user timed tasks Method 2: [root@kylin-xu cron]# cat /var/spool/cron/root
[root@kylin-xu cron]# crontab -l -u root
* * * * * * echo test >>

Case 2. Perform time synchronization every 5 minutes

*/5 * * * * * ntpdate
[root@kylin-xu cron]# crontab -l -u root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin
* * * * * * * echo test >>
* * * * * * ntpdate &>> /root/

Note: You want to keep the results of the timed task directed to the file
* * * * * * ntpdate &>> /root/
If you do not want to keep the results
* * * * * * ntpdate &>/dev/null


Note: With centos, if the mail service is turned off, a small file will be automatically generated for each timed task, and the small file will take up the inode number.
[root@linuxn ~]# systemctl stop postfix
Clean up the following directory: /var/spool/postfix
/var/spool/postfix/maildrop/
Alternatively, you can direct the results of the timed task to a file or an empty directory.