[CentOS] 解决 crontab 无法读取环境变量的问题
2017-12-26 20:57
417 查看
1. 问题描述
一段数据处理的 shell 程序,在 shell 中手动运行,可以正确执行。但是,把它放在 crontab 列表里,就会报错,提示 "matlab: command not found."。AutoRefreshData.sh 的部分内容如下:
[She@She ~]$ cat /home/She/data/AutoRefreshData.sh #!/bin/bash ... MatlabFile='/mnt/L/Data/main4mat.m' chmod +x ${MatlabFile} matlab -nodesktop -nosplash -nojvm < ${MatlabFile} 1>running.log 2>running.err &
在终端下,AutoRefreshData.sh 可正确执行:
[She@She ~]$ /home/She/data/AutoRefreshData.sh [She@She ~]$ cat ~/running.log < M A T L A B (R) > Copyright 1984-2015 The MathWorks, Inc. R2015b (8.6.0.267246) 64-bit (glnxa64) August 20, 2015 For online documentation, see http://www.mathworks.com/support For product information, visit www.mathworks.com. >> >> >> >> >> >> >> /mnt/L/Data/matFile/jpl16228.mat >>
[She@She ~]$ cat ~/running.err [She@She ~]$
将该 shell 脚本添加到 crontab 中:
[She@She ~]$ crontab -l # part 2: refresh She data from FTP 08 12 * * * /home/She/data/AutoRefreshData.sh > /dev/null 2>&1
在 crontab 中,运行报错,结果如下:
[She@She ~]$ cat ~/running.log [She@She ~]$ cat ~/running.err /home/She/data/AutoRefreshData.sh: line 111: matlab: command not found
2. Bug 原因分析与修复
原因分析:crontab 有一个坏毛病, 就是它总是不会缺省的从用户 profile 文件中读取环境变量参数,经常导致在手工执行某个脚本时是成功的,但是到 crontab 中试图让它定期执行时就是会出错。修复:在脚本文件的开头,强制要求导入环境变量,可保万无一失。
这样的话,脚本的头部一律以下列格式开头:
#!/bin/sh . /etc/profile . ~/.bash_profile
以 AutoRefreshData.sh 为例,它的头部则由
[She@She ~]$ cat /home/She/data/AutoRefreshData.sh #!/bin/bash ... MatlabFile='/mnt/L/Data/main4mat.m' chmod +x ${MatlabFile} matlab -nodesktop -nosplash -nojvm < ${MatlabFile} 1>running.log 2>running.err &
改为:
[She@She ~]$ vi /home/She/data/AutoRefreshData.sh
#!/bin/sh . /etc/profile . ~/.bash_profile...
MatlabFile='/mnt/L/Data/main4mat.m'
chmod +x ${MatlabFile}
matlab -nodesktop -nosplash -nojvm < ${MatlabFile} 1>running.log 2>running.err &
之后,更新 crontab 中的运行时间,立即测试,一切正常,不再报错。
[She@She ~]$ cat ~/running.log < M A T L A B (R) > Copyright 1984-2015 The MathWorks, Inc. R2015b (8.6.0.267246) 64-bit (glnxa64) August 20, 2015 For online documentation, see http://www.mathworks.com/support For product information, visit www.mathworks.com. >> >> >> >> >> >> >> /mnt/L/Data/matFile/jpl16228.mat >> [She@She ~]$ cat ~/running.err [She@She ~]$
Done。
相关文章推荐
- [CentOS] 解决 crontab 无法读取环境变量的问题【转】
- [CentOS] 解决 crontab 无法读取环境变量的问题
- 解决 Crontab 无法正确读取 PATH 环境变量的土办法
- 解决crontab无法读取环境变量
- 解决crontab不加载环境变量问题
- ubuntu下使用eclipse调试jni无法获取环境变量,本地库(java.library.path,LD_LIBRARY_PATH)等问题的解决。
- Mac下IDE无法读取环境变量问题
- 在java.ext.dirs中使用环境变量导致crontab执行不成功的问题及解决
- adb.exe无法启动 和配置Maven、JDK环境变量的问题的解决
- Crontab内环境变量与Shell环境变量的关系及解决问题的办法
- centos下解决ssh hostname command 环境变量问题
- 解决CentOS下MySQL数据库无法正常被读取,windows下正常的问题
- 亲测!JDK环境变量设置——或找不到或无法加载主类con.sun.tools.javac.Main的问题解决
- mac os x10.11.2系统eclipse无法读取环境变量的问题
- crontab无法执行-环境变量问题
- QT Creator + qt4.8.6配置报错:无法在环境变量中找到make命令: mingw32-make.exe问题解决
- android studio在添加了jdk环境变量后依然无法打开问题解决
- centos crontab环境变量问题
- Linux Crontab内环境变量与Shell环境变量的关系及解决问题的办法
- crontab 里面这么写可以解决环境变量问题