CentOS 使用 Cron定时服务

CentOS 使用 Cron定时服务

  1. 查看cron服务启动状态
systemctl status crond
  1. 启动cron服务
systemctl restart crond
  1. 编辑要执行命令
crontab -e
# 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,$
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

例:
每分钟检查一次

*/1 * * * * bash /home/check.sh

在每天5点的时候,每10分钟执行一次

*/10 5 * * * bash /home/backup_db.sh
  1. 查看要执行命令
crontab -l
  1. 脚本编写
nano backup_db.sh
#!/bin/bash

cd /backup_folder

function backupSql() {
     
        currentTime=`date "+%Y-%m-%d-%H-%M-%S"`

        dbName=$1
        backupTime=$2

        dumpFile=/backup_folder/$dbName-$backupTime.dump

        echo $currentTime: dumpFile: $dumpFile >> ./backup.log

        if [ ! -f $dumpFile ];then
                echo $currentTime: backupSql: $dumpFile  >> ./backup.log
                mysqldump --defaults-extra-file=/etc/my.cnf -R $dbName > ./$dumpFile
        fi
}

ts=`date "+%Y-%m-%d-%H-%M-%S"`
echo $ts: Cornd check backup database! >> ./backup.log

# backup database
if [ ! -d "./backup_folder" ];then
        mkdir ./backup_folder
fi

bkh=`date "+%H"`

if [ $bkh == '05' ]; then
        dumpTime=`date "+%Y-%m-%d-%H"`
        echo $ts: backup database! >> ./backup.log
        backupSql "db_name" $dumpTime
fi

你可能感兴趣的:(工具使用,CentOS)