参考资料:http://blog.slogra.com/post-238.htmlhtml
一段数据处理的 shell 程序,在 shell 中手动运行,能够正确执行。可是,把它放在 crontab 列表里,就会报错,提示 "matlab: command not found."。node
AutoRefreshData.sh 的部份内容以下:shell
[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 可正确执行:bash
[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 中:jvm
[She@She ~]$ crontab -l
# part 2: refresh She data from FTP 08 12 * * * /home/She/data/AutoRefreshData.sh > /dev/null 2>&1
在 crontab 中,运行报错,结果以下:post
[She@She ~]$ cat ~/running.log
[She@She ~]$ cat ~/running.err
/home/She/data/AutoRefreshData.sh: line 111: matlab: command not found
缘由分析:crontab 有一个坏毛病, 就是它老是不会缺省的从用户 profile 文件中读取环境变量参数,常常致使在手工执行某个脚本时是成功的,可是到 crontab 中试图让它按期执行时就是会出错。测试
修复:在脚本文件的开头,强制要求导入环境变量,可保万无一失。spa
这样的话,脚本的头部一概如下列格式开头:code
#!/bin/sh . /etc/profile . ~/.bash_profile
以 AutoRefreshData.sh 为例,它的头部则由orm
[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。