Linux crontab定时任务案例

   

一、案例:先创建一个shell脚本 :

    本例是删除指定文件夹内N天前的文件

#!/bin/sh
if [ -f ~/.bash_profile ];
then
  . ~/.bash_profile
fi

location="/tmp/testfile/"
find $location -mtime +40 -type f|xargs rm -f
删除N天前文件脚本:delfileAgoDays.sh

二、定义定时任务表


   查看、编辑和删除cron把命令行,保存在crontab(cron table)文件里,该文件通常在 /etc 目录下。每个系统用户都可以有自己的crontab(在 /var/spool/cron/ 下):

[root@TEST144239 cron]# crontab -e

30 12 * * * /root/cron_file/delfileAgoDays.sh

[root@TEST144239 /]# cd /var/spool/cron/
[root@TEST144239 cron]# ll

总用量 4
-rw------- 1 root root 47 11月 12 11:53 root
定义定时任务表

  如果一个cron任务需要定期而不是按小时,天,周,月来执行,则需要添加/etc/cron.d目录。这个目录下的所有文件和文件/etc/crontab语法相同:

三、将置cron 服务设置为系统自启动服务

  cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业。由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动、关闭这个服务:

/sbin/service crond start //启动服务
/sbin/service crond stop //关闭服务
/sbin/service crond restart //重启服务
/sbin/service crond reload //重新载入配置

  将这个服务在系统启动的时候自动启动:

  在/etc/rc.d/rc.local这个脚本的末尾加上:
  

  

[root@TEST144239 etc]# vi rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
/sbin/service crond start
rc.local

四、配置cron 服务的开机运行级别

      

      如果让crond 在开机时运行,应该改变其运行级别;

      [root@TEST144239 ~]# chkconfig --levels 35 crond on

五.案例:

5.1准备备份数据库脚本:

connect target sys/Sina.2015@study 
CONFIGURE RETENTION POLICY TO REDUNDANCY 2;
run {
allocate channel d1 device type disk;
backup
full
format '/data2/backup/%d_%s_%p_%t.db'
tag dbfull
database;
sql 'alter system archive log current';
backup archivelog all
format '/data2/backup/%d_%s_%p_%t.arch'
tag dbarch;
backup spfile
format '/data2/backup/%d_%s_%p_%t.spfile'
tag dbspfile;
backup
format '/data2/backup/%d_%s_%p_%t.ctl'
tag dbctl
current controlfile;
release channel d1;
}
crosscheck backup;
delete noprompt expired backup;
delete noprompt obsolete;
备份数据库脚本:dbbackup.rcv
export ORACLE_SID=orcl
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1
export PATH=$PATH:$ORACLE_HOME/bin
rman nocatalog cmdfile=/home/oracle/cron_file/dbbackup.rcv msglog=/home/oracle/cron_file/dbbackup.log
运行备份的sh:bakup_full_nas.sh

   若出现错误信息:

    Message file RMAN<lang>.msb not found

    Verify that ORACLE_HOME is set properly

  可以尝试:

export ORACLE_OWNER=oracle
export ORACLE_SID=orcl
/u01/app/oracle/product/11.2.0/db_1/bin/rman nocatalog cmdfile=/home/oracle/cron_file/dbbackup.rcv msglog=/home/oracle/cron_file/dbbackup.log
View Code

5.2将脚本上传到oracle用户的目录中:

[root@TEST144239 /]# cd /home/oracle/cron_file
[root@TEST144239 cron_file]# ll
总用量 8
-rwxrwxr-x 1 oracle oinstall 253 11月 12 16:06 backup_full_nas.sh
-rwxrwxr-x 1 oracle oinstall 578 11月 12 15:50 dbbackup.rcv
[root@TEST144239 cron_file]# 
查询脚本

5.3 在oracle用户下添加cron定时服务

[oracle@TEST144239 ~]$ crontab -e


12 16 * * * /home/oracle/cron_file/backup_full_nas.sh
crontab 添加计划


5.4 重启crond服务

[root@TEST144239 cron_file] service crond restart
重启cron服务

     

     

    

原文地址:https://www.cnblogs.com/HondaHsu/p/1888192.html