Cron 作业未运行

Cron 作业未运行

甲骨文Linux 5.10

我可以以 oracle 用户身份成功手动运行脚本“tblspc_usage.sh”。我知道这一点是因为它通过电子邮件向我发送了一份报告。

但它根本不从 cron 运行。它不生成日志文件。

[oracle@dub-ImrORA3 scripts]$ crontab -l
# user /bin/sh
SHELL=/bin/sh
# mail to oracle user
MAILTO=oracle
# run at 9:45 AM monday thur friday
45 09 * * 1-5 /home/oracle/scripts/tblspc_usage.sh 2>&1 /home/oracle/scripts/tblspc_usage.log

[oracle@dub-ImrORA3 scripts]$ ls -al tblspc_usage.sh
-rwxrwxr-- 1 oracle oinstall 2013 Jan 20 09:12 tblspc_usage.sh

好的,这是 /var/mail/oracle /home/oracle/scripts/tblspc_usage.sh 中的电子邮件:第 15 行:sqlplus:找不到命令 grep:body.log:没有这样的文件或目录

这是我的 shell 脚本:

#!/bin/sh
#
# tblspc_usage.sh
#=======================================
#
# Checks for tablespace usage exceeding 90% and email the details
# does not check undo or temp tablespaces
#=======================================
#
ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1 ; export ORACLE_HOME
ORACLE_SID=IMR1 ; export ORACLE_SID
user="system"
pass="letmein" # bogus passwd

sqlplus -S $user/$pass <<EOF
   column "TOTAL ALLOC (MB)"      format 9,999,990.00
   column "TOTAL PHYS ALLOC (MB)" format 9,999,990.00
   column "USED (MB)"             format 9,999,990.00
   column "FREE (MB)"             format 9,999,990.00
   column "% USED"                format 990.00
   set echo off
   spool body.log
   select 
       a.tablespace_name,
       a.bytes_alloc/(1024*1024)               "TOTAL ALLOC (MB)",
       a.physical_bytes/(1024*1024)            "TOTAL PHYS ALLOC (MB)",
       nvl(b.tot_used,0)/(1024*1024)           "USED (MB)",
       (nvl(b.tot_used,0)/a.bytes_alloc)*100   "USED %"
   from 
       (select 
            tablespace_name,
            sum(bytes) physical_bytes,
            sum(decode(autoextensible,'NO',bytes,'YES',maxbytes)) bytes_alloc
        from 
            dba_data_files
        group by 
            tablespace_name ) a,
   (select 
        tablespace_name, 
        sum(bytes) tot_used
    from 
        dba_segments
    group by 
        tablespace_name ) b
  where 
        a.tablespace_name = b.tablespace_name (+)
  and 
        a.tablespace_name not in 
           (select distinct 
                   tablespace_name 
            from 
                   dba_temp_files
           )
  and      
        a.tablespace_name not like 'UNDO%'
  and
       ( nvl(b.tot_used,0)/a.bytes_alloc)*100 >= 90.00
  order by 1;

    spool off
    exit
EOF

   # if the word "TABLESPACE" exists in the spool file 
   # then at least one tablespace has usage over 90%
   if grep -q TABLESPACE "body.log";
   then
        cat /home/oracle/scripts/body.log | mailx -s "ORA3 - Tablespace(s) 90% Full" \
        [email protected]
   fi

答案1

消息sqlplus: command not found给出了答案。由于您设置了环境变量 ORACLE_HOME,因此在导出 ORACLE_HOME 后将此行放入脚本中:

export PATH=$PATH:$ORACLE_HOME/bin

我还迷信导出TNS_ADMIN:

export TNS_ADMIN=$ORACLE_HOME/network/admin

我不知道这是否是绝对必要的。

答案2

似乎您用更多信息更新了您的帖子,就像布鲁斯所说,该错误告诉您ORACLE_HOME路径丢失。更新您的脚本以包括:

export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1
export PATH=$PATH:$ORACLE_HOME/bin
export ORACLE_SID=IMR1

我仍然会在你的 cron 中修复这个问题:

# run at 9:45 AM monday thur friday
45 09 * * 1-5 /home/oracle/scripts/tblspc_usage.sh >> /home/oracle/scripts/tblspc_usage.log 2>&1
  • 附加到日志:>>
  • 将 stderr 重定向到日志文件(在本例中与 stdout 相同):2>&1

相关内容