crontab失败的解决过程

1.问题描述:
在使用crontab定时执行发布包sql脚本解析工作时,出现了一个奇怪的问题,
脚本执行到生成release_xxxx.tmp文件就终止执行了,且release_xxxx.tmp
文件是空的。但是以同样的方式手动执行dbmisProject.sh脚本却可以正常执行。

2.解决办法
将crontab的输出结果重定向到一个日志文件中,以记录crontab执行的过程中发生了什么错误。
(1)修改现有的crontab任务
0 3 * * 4 sh /home/dbmisTool/bin/dbmisProject.sh > /dev/null 2>&1

0 3 * * 4 sh /home/dbmisTool/bin/dbmisProject.sh > /tmp/dbmisProject.log 2>&1
这样,只要在周四查看dbmisProject.log文件即知道crontab任务失败的原因了。


(2)周四查看dbmisProject.log中的内容如下:
/home/dbmisTool/bin/dbmisProject.sh: line 118: svn: command not found
ls: /disk2/db_release: No such file or directory
ls: /disk2/db_release: No such file or directory
ls: /disk2/db_release: No such file or directory
ls: /disk2/db_release: No such file or directory
ls: /disk2/db_release: No such file or directory
ls: /disk2/db_release: No such file or directory
ls: /disk2/db_release: No such file or directory
ls: /disk2/db_release: No such file or directory
/home/dbmisTool/bin/dbmisProject.sh: line 185: sqlplus: command not found
cat: /home/dbmisTool/tmp/report_20091126.txt: No such file or directory
Null message body; hope that's ok
原来是环境变量的问题,crontab在执行的时候只会定义少数环境变量而不是继承用户shell环境中的环境变量。因为svn: command not found和sqlplus: command not found说明找不到命令,其实是找不到这两个命令对应的可执行文件所在的目录。

(3)在脚本中添加了source /root/.bash_profile命令,在crontab执行脚本时将root用户的环境变量加载进去。
发现dbmisProject.log中的报错信息已经由
/home/dbmisTool/bin/dbmisProject.sh: line 118: svn: command not found
ls: /disk2/db_release: No such file or directory
ls: /disk2/db_release: No such file or directory
ls: /disk2/db_release: No such file or directory
ls: /disk2/db_release: No such file or directory
ls: /disk2/db_release: No such file or directory
ls: /disk2/db_release: No such file or directory
ls: /disk2/db_release: No such file or directory
ls: /disk2/db_release: No such file or directory
/home/dbmisTool/bin/dbmisProject.sh: line 185: sqlplus: command not found
cat: /home/dbmisTool/tmp/report_20091126.txt: No such file or directory
Null message body; hope that's ok
变为
/home/dbmisTool/bin/dbmisProject.sh: line 123: svn: command not found
ls: /disk2/db_release: No such file or directory
ls: /disk2/db_release: No such file or directory
ls: /disk2/db_release: No such file or directory
ls: /disk2/db_release: No such file or directory
ls: /disk2/db_release: No such file or directory
ls: /disk2/db_release: No such file or directory
ls: /disk2/db_release: No such file or directory
ls: /disk2/db_release: No such file or directory
cat: /home/dbmisTool/tmp/report_20091203.txt: No such file or directory
Null message body; hope that's ok

即sqlplus命令已经可以找到,但是svn命令还是无法找到,但是使用man svn是可以查看到的帮助信息的。which指令会在环境变量$PATH设置的目录里查找符合条件的文件。
使用which命令查看svn所在的目录:
[root@db_dev_bak disk2]# which svn
/usr/local/bin/svn
于是将svn所在的路径加在了svn前面,即对svn命令作如下修改:
修改原命令
svn co --username=xxxxx --password=xxxx http://svn.xxxx.net/xxxx/socdocs/trunk/devdba/db_release/

/usr/local/bin/svn co --username=xxxxx --password=xxxx http://svn.xxxx.net/xxxx/socdocs/trunk/devdba/db_release/
再看此时crontab任务生成的的dbmisProject.log,之前的svn: command not found错误已经消失,取而代之的是一个新的错误:
svn: Can't convert string from 'UTF-8' to native encoding:
svn: db_release/20090701?\232?\161?\165
cat: /home/dbmisTool/tmp/report_20091203.txt: No such file or directory
Null message body; hope that's ok

cron job在执行的时候只会定义少数环境变量而不是继承用户shell环境中的环境变量,所以在svn碰到含有cron job环境中
设定的字符集不能处理的字符的文件名时会导致检出失败。
解决方法就是在脚本中加入:
export LC_CTYPE=zh_CN.GB18030
之前在网上看到有的是加载en_US.UTF-8字符集,但是我试了一下,在系统上显示是乱码,这个还是要因系统而异。
经过以上对脚本的修改,终于大功告成,定时任务可以正常运行了。

PS:在完成对这个定时任务的修正以后得到了一个更为简单的方法,即只要在脚本中加入以下即可:

source /etc/profile
source /etc/sysconfig/i18n

 


 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/21805468/viewspace-621602/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/21805468/viewspace-621602/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值