在工作中经常会遇到一些状况,比如写好的 shell 脚本手工运行很正常,但一旦把其配置在 crontab 上调度就会出现这样或那样的问题。本人就遇到到如下几种情况。
1 、在调用 oracle 的 sqlplus 、 sqlldr 等命令工具时必须写出其全路径才能在 crontab 中执行成功,否则,虽然手动运行很正常,但一配到 crontab 上就出现异常。
2 、手动运行 shell 脚本时,可以用 sh 命令;但在 cron 中一定不能用 sh 执行命令,而要用直接的列出 shell 脚本文件的方式顺序执行。
3 、在 crontab 调用时,如果有用到数据库,最好把数据库的相关环境变量等列写出来。
4 、要在 crontab 里调度, shell 脚本中引用到的文件最好都写绝对路径。
5、如果是ORACLE数据库的脚本,一定要注意环境变量,例如
PATH=$PATH:$HOME/bin
export PATH
export TMP=/tmp
export TMPDIR=/tmp
export ORACLE_BASE=/oracle/app
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_1
export ORACLE_SID=racdb1
export PATH=$ORACLE_HOME/bin:/usr/sbin:$PATH
export NLS_LANG="SIMPLIFIED CHINESE_CHINA.ZHS16GBK"
6、 检查crond进程是否正常
/etc/init.d/crond status (貌似centos7版本没有这个命令)
/etc/init.d/crond start
service rsyslog status
ps -elf|grep crond
7、如何判断crontab是否执行,可以看这个日志 /var/log/cron
8、如果是root用户下执行,环境变量可以设置加上这两行
. /etc/profile
. ~/.bash_profile
9、如果是grails进程的启动
nohup grails prod run-app -Dserver.port=xxxx --non-interactive 2>&1 > nohup.out &
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28371090/viewspace-3159083/ ,如需转载,请注明出处,否则将追究法律责任。