linux中shell脚本直接执行没有问题,crontab定时执行失败的解决方法

Linux下用crontab执行定时任务不会缺省的从用户profile文件中读取环境变量参数,经常导致在手工执行某个脚本时是成功的,但是到crontab中试图让它定期执行时就是会出错。

这是因为用户登陆Linux操作系统的时候,”/etc/profile”, “~/.bash_profile”等配置文件会被自动执行。
有2种方法可以解决这个问题,

1、在Shell文件里面获取环境变量值的路径写成绝对路径,别用环境变量的路径值。

例如获取CPU的使用情况  通过绝对路径/proc/cpuinfo 来获取值;

2、Shell脚本缺省的#!/bin/bash开头换行后的第一行用(推荐使用)

#!/bin/bash

. /etc/profile

. ~/.bash_profile

这样,crontab在执行脚本的时候,就能够读到用户的环境变量参数.
 

![图片说明](https://img-ask.csdn.net/upload/201903/19/1552974770_644010.png)脚本如图所示,每分钟执行自动启动ESB,现在打印的日志执行了,脚本执行。但是手动执行restart.sh脚本是可以成功的。ESB版本:8.0.0.2 /etc/profile如下: ![图片说明](https://img-ask.csdn.net/upload/201903/19/1552974897_595536.png)![图片说明](https://img-ask.csdn.net/upload/201903/19/1552974901_967642.png) .bash_profile如下:![图片说明](https://img-ask.csdn.net/upload/201903/19/1552974964_650536.png) . /opt/ibm/mqsi/8.0.0.2/bin/mqsiprofile如下: #!/bin/bash # Licensed Materials - Property of IBM # 5724-A82 # (c) Copyright IBM Corp. 2012. # All Rights Reserved # US Government Users Restricted Rights - use, # duplication or disclosure restricted by GSA # ADP Schedule Contract with IBM Corp. # Level: S800-FP02 # Check that this script was sourced and not executed. if test "`basename $0 2>/dev/null`" = "mqsiprofile" then echo mqsiprofile must be run in the context of this shell, using the \"source\" or dot \".\" notation, not simply run as a script. echo Try using \". $0\" or \"source $0\" from the current context. exit 1 fi if [ -n "${PROSPECTIVE_MQSI_FILEPATH+MQSI_PROFILE_NOT_SET}" ]; then echo mqsiprofile repetition disallowed return 1 fi PROSPECTIVE_MQSI_FILEPATH=/opt/ibm/mqsi/8.0.0.2 export PROSPECTIVE_MQSI_FILEPATH MQSI_FILEPATH=${PROSPECTIVE_MQSI_FILEPATH} export MQSI_FILEPATH MQSI_WORKPATH=/var/mqsi export MQSI_WORKPATH MQSI_VERSION=8.0.0.2 MQSI_VERSION_V=8 MQSI_VERSION_R=0 MQSI_VERSION_M=0 MQSI_VERSION_F=2 MQSI_FAD=6 export MQSI_VERSION MQSI_VERSION_V MQSI_VERSION_R MQSI_VERSION_M MQSI_VERSION_F MQSI_FAD MQSI_PROCESSOR_ARCHITECTURE=64 export MQSI_PROCESSOR_ARCHITECTURE # If there is a primary MQ install, ensure that its environement is consistent if [ -r ${MQ_INSTALLATION_PATH:-/usr}/bin/setmqenv ]; then . ${MQ_INSTALLATION_PATH:-/usr}/bin/setmqenv -s elif [ -r /usr/bin/setmqenv ]; then . /usr/bin/setmqenv -s elif [ -r /opt/mqm/bin/dspmqver ]; then MQ_JAVA_INSTALL_PATH=/opt/mqm/java MQ_INSTALLATION_PATH=/opt/mqm export MQ_JAVA_INSTALL_PATH export MQ_INSTALLATION_PATH fi MQSI_REGISTRY=${MQSI_WORKPATH} export MQSI_REGISTRY PATH=${MQSI_FILEPATH}/bin:${MQSI_FILEPATH}/isadc${PATH:+":"${PATH}} export PATH MQSI_LILPATH=${MQSI_FILEPATH}/lil:${MQSI_FILEPATH}/jplugin${MQSI_LILPATH:+":"${MQSI_LILPATH}} export MQSI_LILPATH MQSI_SECURITY_PROVIDER_PATH=${MQSI_FILEPATH}/SecurityProviders${MQSI_SECURITY_PROVIDER_PATH:+":"${MQSI_SECURITY_PROVIDER_PATH}} export MQSI_SECURITY_PROVIDER_PATH MQSI_EXMLTCONFIGPATH=${MQSI_FILEPATH}/exmltConfig export MQSI_EXMLTCONFIGPATH LD_LIBRARY_PATH=${MQSI_FILEPATH}/xml4c/lib:${MQSI_FILEPATH}/lib:${MQSI_FILEPATH}/bin:${MQSI_FILEPATH}/ODBC/V6.0/lib:${MQSI_FILEPATH}/xlxpc/lib:${MQSI_FILEPATH}/dfdlc/lib${LD_LIBRARY_PATH:+":"${LD_LIBRARY_PATH}} export LD_LIBRARY_PATH ICU_DATA=${MQSI_FILEPATH}/xml4c/data${ICU_DATA:+":"${ICU_DATA}} export ICU_DATA NLSPATH=${MQSI_FILEPATH}/messages/%L/%N:${MQSI_FILEPATH}/messages/En_US/%N${NLSPATH:+":"${NLSPATH}} export NLSPATH MQSI_JARPATH=${MQSI_FILEPATH}/classes:${MQSI_FILEPATH}/messages${MQSI_JARPATH:+":"${MQSI_JARPATH}} export MQSI_JARPATH MQSI_CATALINA_HOME=${MQSI_FILEPATH}/catalina export MQSI_CATALINA_HOME MQSI_WEBADMIN_HOME=${MQSI_FILEPATH}/webadmin export MQSI_WEBADMIN_HOME CLASSPATH=${MQSI_FILEPATH}/messages:${MQSI_FILEPATH}/classes:${MQSI_FILEPATH}/classes/ConfigManagerProxy.jar:${MQSI_FILEPATH}/classes/brokerutil.jar${CLASSPATH:+":"${CLASSPATH}}:${MQSI_WORKPATH}/common/wsrr export CLASSPATH IBM_JVM_SR_SIGNUM=64 export IBM_JVM_SR_SIGNUM MIBDIRS=${MQSI_FILEPATH}/snmp-mib${MIBDIRS:+":"${MIBDIRS}} export MIBDIRS for SCRIPT in ${MQSI_WORKPATH}/common/profiles/*.sh; do if [ -r "${SCRIPT}" ]; then if [ "${SCRIPT}" != "${MQSI_WORKPATH}/common/profiles/IE02.sh" ]; then . ${SCRIPT} fi fi done # JRE path overrides in custom profiles are not allowed from 7.0 onwards MQSI_JREPATH=${MQSI_FILEPATH}/jre16 export MQSI_JREPATH PATH=${MQSI_JREPATH}/bin${PATH:+":"${PATH}} export PATH LD_LIBRARY_PATH=${MQSI_JREPATH}/lib/amd64:${MQSI_JREPATH}/lib/amd64/classic${LD_LIBRARY_PATH:+":"${LD_LIBRARY_PATH}} export LD_LIBRARY_PATH echo echo MQSI $MQSI_VERSION echo $MQSI_FILEPATH echo 请各位大佬帮忙看看问题出在哪里,急需帮助!!!
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页