linux - scheduled tasks

1. Schedule tasks

        Plan a certain time point in advance to execute a certain task, so the function of the scheduled task is to do some periodic tasks. In the work production, the scheduled task is mainly used for timing backup and cleaning data.

        Advantages: 1. It can be executed automatically, which improves work efficiency and liberates manpower. 2. Any script can be executed with scheduled tasks.

2. Use of commands

        2.1 Edit scheduled tasks

                Use the crontab -e command to edit the scheduled tasks of the current user. After entering the editor, write according to the standardized format, that is, * * * * * execute the command , then save and exit. Each user has its own scheduled tasks.

         2.2 Delete scheduled tasks

                There are two ways to delete scheduled tasks, one is to use the crontab -r command to delete directly, the second is to use the traditional method, that is, use the crontab -e command to enter the scheduled task editing interface, and comment out the no longer needed scheduled tasks can

        2.3 View scheduled tasks

                Use the crontab -l command to view the scheduled tasks of the current user, and you can also use the crontab -l -u user command to view the scheduled tasks of the specified user, provided that your permissions allow you to view the scheduled tasks of other users

        2.4 Diagram (more notes)

 3. Log files for scheduled tasks

        When the scheduled task executes the script, it is executed in the background and cannot be seen by people. However, by monitoring the crontabd log file to see whether the scheduled task is executed (ie, the command tail -l /var/log/cron ), the crontab log (/var/log/cront): record which scheduled tasks are created, executed, modified, View and other operations.

Mar 16 15:06:35 sanchuang crontab[22483]: (root) LIST (root)  查看
Mar 16 15:06:49 sanchuang crontab[22484]: (root) BEGIN EDIT (root)  编辑
Mar 16 15:07:07 sanchuang crontab[22484]: (root) REPLACE (root) 修改
Mar 16 15:07:07 sanchuang crontab[22484]: (root) END EDIT (root)  结束编辑
Mar 16 15:07:25 sanchuang crontab[22486]: (root) LIST (root)
Mar 16 15:08:01 sanchuang crond[22151]: (root) RELOAD (/var/spool/cron/root)  加载计划任务开始执行
Mar 16 15:08:01 sanchuang CROND[22491]: (root) CMD (bash /lianxi/sanchuang/create_dir.sh)
Mar 16 15:08:01 sanchuang CROND[22489]: (root) CMDOUT (tar: 从成员名中删除开头的“/”)  脚本的执行输出效果

CROND[22491] CROND is the process[22491] is the process number pid number of crond, and CMD means to execute the command command

        /var/spool/cron is used to store the user's scheduled tasks          /etc/cron.d/  directory for storing scheduled tasks         /etc/cron.daily/stores tasks that need to be performed every day         /etc/cron.hourly/stores hourly needs Tasks to be executed         /etc/cron.monthly/stores the tasks that need to be performed every month         /etc/cron.weekly/stores  the tasks that need to be performed every week         /etc/cron.deny  stores who refuses to execute the scheduled tasks

4. Hackers obtain data

        

5.anacron

        The anacron service is a supplementary program of the cron service, which can check for leaks and fill in gaps. It makes up for the problem that cron cannot execute scheduled tasks after the system is shut down, and executes the scheduled tasks that have not been executed after the system is turned on. It cannot replace cron to check the cron tasks that the system has not performed on a daily, weekly or monthly basis. The configuration file is: /var/spool/anacron. Service name: /etc/init.d/anacrond
        automatically runs when booting, and then executes the unexecuted scheduled tasks, anacron will automatically stop cat /etc/anacrontab 

6. Use cases

        Example: 5. Create a new directory /backcup as the directory that needs to back up files later. Back up /etc/passwd, /var/log, /boot to the backup directory and package it as 2017-3-29_pwd_log_boot.tar.xz. Just add the date, month, and day to the file name.
Back up once every day at 23:00, using scheduled tasks to execute. The files of the last 20 days are kept, and the files older than 20 days are deleted. The script name is backup_pwd_boot_log.sh

1. Create a backup directory:

mkdir /backup

2. Create a backup script file:

touch /usr/local/bin/backup_pwd_boot_log.sh
chmod +x /usr/local/bin/backup_pwd_boot_log.sh

3. Edit the backup script file and package the files to be backed up:

#!/bin/bash

# 备份文件所在目录
BACKUP_DIR="/backup"

# 需要备份的文件
FILES_TO_BACKUP=(
    "/etc/passwd"
    "/var/log"
    "/boot"
)

# 打包并压缩备份文件
BACKUP_FILE_NAME="$(date +'%Y-%m-%d')_pwd_log_boot.tar.xz"
tar -cJf "${BACKUP_DIR}/${BACKUP_FILE_NAME}" "${FILES_TO_BACKUP[@]}"

# 删除20天之前的备份文件
find "${BACKUP_DIR}" -type f -name "*_pwd_log_boot.tar.xz" -mtime +19 -delete

4. Add scheduled tasks:

crontab -e

5. Then add the following:

# 每天23:00执行备份脚本
0 23 * * * /usr/local/bin/backup_pwd_boot_log.sh

In this way, the scheduled task is created successfully, but you need to pay attention to the permissions of the file

If you are not familiar with the compression command, you can read the explanation of the compression command I wrote before:

https://mp.csdn.net/mp_blog/creation/editor/new/129740504

                Try writing the corresponding time:

        

The answer is as follows:         

Guess you like

Origin blog.csdn.net/m0_53891399/article/details/130959500