欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【Do家】Linux下crontab执行非os自带命令注意事项 centoscrontabjstat

程序员文章站 2022-03-03 11:06:47
...
【场景】通过crontab执行java命令

【问题】尽管已设置/etc/profile和/root/.bash_profile中环境变量
////////begin//////
# remedied by can_do on 2020-03-07
export JAVA_HOME=/opt/jdk
export PATH=${JAVA_HOME}/bin:${PATH}
export CLASSPATH=.:${JAVA_HOME}/lib/tools.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/jre/lib/rt.jar
////////end////////

但crontab中设置的java命令还是不能执行,而在root用户的shell窗口下是可以成功执行java命令的,说明环境变量配置是OK。


////////////execute in failure///////begin/////
From root@cnd-centos.localdomain  Thu Jun 24 20:42:02 2021
Return-Path: <root@cnd-centos.localdomain>
X-Original-To: root
Delivered-To: root@cnd-centos.localdomain
Received: by cnd-centos.localdomain (Postfix, from userid 0)
id 2A2A761C20; Thu, 24 Jun 2021 20:42:02 +0800 (CST)
From: "(Cron Daemon)" <root@cnd-centos.localdomain>
To: root@cnd-centos.localdomain
Subject: Cron <root@cnd-centos> /opt/shell/yourShellScript.sh
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=13>
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: <20210624124202.2A2A761C20@cnd-centos.localdomain>
Date: Thu, 24 Jun 2021 20:42:02 +0800 (CST)

/opt/shell/yourShellScript.sh: line 9: jstat: command not found
/opt/shell/yourShellScript.sh: line 11: jstat: command not found
The current pid is 1876.
Eden percent is .
Old percent is .
/opt/shell/yourShellScript.sh: line 21: [: -gt: unary operator expected
////////////execute in failure///////end/////


【解决方法】
显示告诉os,通过root用户执行java命令,如下:

su - -c "/opt/shell/yourShellScript.sh"


////////////execute in success/////////begin///////
From root@cnd-centos.localdomain  Thu Jun 24 20:44:02 2021
Return-Path: <root@cnd-centos.localdomain>
X-Original-To: root
Delivered-To: root@cnd-centos.localdomain
Received: by cnd-centos.localdomain (Postfix, from userid 0)
id 5A47961C20; Thu, 24 Jun 2021 20:44:02 +0800 (CST)
From: "(Cron Daemon)" <root@cnd-centos.localdomain>
To: root@cnd-centos.localdomain
Subject: Cron <root@cnd-centos> su - -c "/opt/shell/yourShellScript.sh"
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=15>
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: <20210624124402.5A47961C20@cnd-centos.localdomain>
Date: Thu, 24 Jun 2021 20:44:02 +0800 (CST)

The current pid is 1876.
Eden percent is 20.
Old percent is 3.
////////////execute in success/////////end///////

【小结】
1> 引起该问题的原因,是多用户下导致的环境变量选择问题,su -相当切到root下执行,前提是环境变量已配置ok;
2> Linux启动脚本/etc/rc.local中命令执行不成功,除了chmod +x权限外,也需要调整为su -  -c "yourScript"
3> 通过系统日志分析问题原因tail -f /var/log/boot.log,也是关键一步;

【温馨提示】
如果您觉得满意,可以选择支持下,您的支持是我最大的动力:

【Do家】Linux下crontab执行非os自带命令注意事项
            
    
    
        centoscrontabjstat