crontab 定时任务环境变量问题

原创
10/14 11:24
阅读数 33

一.最近在做监控kafka 偏移量监控,遇到一个脑壳疼的问题,在终端执行shell脚本的时候并没有问题,但是放到定时任务里面获取的变量值为空,于是调试将变量值echo 出来,在输出日志看仍然变量值为空,手动执行变量仍然是有值的,一番折腾无果. 翻看 crontab messages 日志仍然没有获取有用的日志,最后再次用手动执行 kafka 偏移量命令时有提示 :You have mail in /var/spool/mail/root,于是看了这个提示文件的内容,最后一行:/usr/local/kafka/bin/kafka-run-class.sh: line 317: exec: java: not found,原来是找不到java命令,这下整明白了,java我这边是手动安装,不是yum安装,/usr/local/kafka/bin/kafka-run-class.sh 里$java 的变量是指向/usr/bin/java

$ less  /var/spool/mail/root

From root@gm-ad-kafka-3.localdomain  Wed Oct 13 11:18:01 2021
Return-Path: <root@gm-ad-kafka-3.localdomain>
X-Original-To: root
Delivered-To: root@gm-ad-kafka-3.localdomain
Received: by gm-ad-kafka-3.localdomain (Postfix, from userid 0)
        id B41E9C5A48; Wed, 13 Oct 2021 11:18:01 +0000 (UTC)
From: "(Cron Daemon)" <root@gm-ad-kafka-3.localdomain>
To: root@gm-ad-kafka-3.localdomain
Subject: Cron <root@gm-ad-kafka-3> /bin/bash /opt/sh/1.sh >> /var/log/crontab/1.log
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=4779>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <20211013111801.B41E9C5A48@gm-ad-kafka-3.localdomain>
Date: Wed, 13 Oct 2021 11:18:01 +0000 (UTC)

/usr/local/kafka/bin/kafka-run-class.sh: line 317: exec: java: not found
vim /usr/local/kafka/bin/kafka-run-class.sh

314 if [ "x$DAEMON_MODE" = "xtrue" ]; then
315   nohup "$JAVA" $KAFKA_HEAP_OPTS $KAFKA_JVM_PERFORMANCE_OPTS $KAFKA_GC_LOG_OPTS $KAFKA_JMX_OPTS $KAFKA_LOG4J_OPTS -cp "$CLASSPATH" $KAFKA_OPTS "$@" > "$CONSOLE_    OUTPUT_FILE" 2>&1 < /dev/null &
316 else
317   exec "$JAVA" $KAFKA_HEAP_OPTS $KAFKA_JVM_PERFORMANCE_OPTS $KAFKA_GC_LOG_OPTS $KAFKA_JMX_OPTS $KAFKA_LOG4J_OPTS -cp "$CLASSPATH" $KAFKA_OPTS "$@"
318 fi
# 服务器java环境变量
$ vim  /etc/profile

JAVA_HOME=/usr/local/jdk1.8/
JAVA_BIN=/usr/local/jdk1.8/bin
JRE_HOME=/usr/local/jdk1.8/jre
export PATH=$PATH:/usr/local/jdk1.8/bin:/usr/local/jdk1.8/jre/bin:/usr/local/protoc/bin
CLASSPATH=/usr/local/jdk1.8/jre/lib:/usr/local/jdk1.8/lib:/usr/local/jdk1.8/jre/lib/charsets

二.将java 软链 到/usr/bin/java,问题解决

$  ln -s /usr/local/jdk1.8/bin/java /usr/bin/java 

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部