# crontab读取环境变量方法

crontab如果不注意的话早晚会出问题，而且这种问题一旦出一次，就会永远记得，因为这种问题很折腾人。
——某前辈

30 0 * * * cd /home/work/user/huangbx/research/getfeature/data/current; sh resample.sh &>/dev/null

$sh resample.sh是可以运行的$head -5 resample.sh
##对事实数据进行采样
set -x
g_date=date -d "3 days ago " +%Y%m%d

crontab中必须十分注意环境变量的使用
#!/bin/sh并不是必须，只是当没有sha-bang的时候，也不要在第一行有"#"后带的中文注释！！

2008-11-3补充：

Traceback (most recent call last):
File "areafile.py", line 2, in <module>
import MySQLdb
File "build/bdist.linux-x86_64/egg/MySQLdb/__init__.py", line 19, in <module>
File "build/bdist.linux-x86_64/egg/_mysql.py", line 7, in <module>
File "build/bdist.linux-x86_64/egg/_mysql.py", line 6, in __bootstrap__
ImportError: libmysqlclient.so.15: cannot open shared object file: No such file or directory

MySQLdb需要调用mysql这个库，可是系统并不知道你的mysql安装在哪里 : (

export LD_LIBRARY_PATH=/home/work/local/mysql5/lib/mysql
（也就是来自~/.bash_profile中的LD_LIBRARY_PATH字段）后程序终于可以在crontab中正常启动。

1) ~/.bash_profile && ~/.bashrc

$cat ~/.bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/bin:/home/work/local/python/lib/python2.5/site-packages/django/bin/:$HOME/bin:/home/work/local/mysql5/bin/;
LD_LIBRARY_PATH=/home/work/local/mysql5/lib/mysql
alias py='/home/work/local/python/bin/python'
export PATH LD_LIBRARY_PATH

.bash_profile和.bashrc的差别
/etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.

/etc/bashrc:为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取.
~/.bash_profile:每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该

~/.bashrc:该文件包含专用于你的bash shell的bash信息,当登录时及每次打开新的shell时,该

~/.bash_logout:当每次退出系统(退出bash shell)时,执行该文件.
/etc/profile是全局性的功能，其中设置的变量作用于所有用户，~/.bash_profile中设置的变量能继承/etc/profile中的变量并作用于用户。
~/.bashrc 是交互式 non-login 方式进入 bash 运行的

export LD_LIBRARY_PATH=/home/work/local/mysql5/lib/mysql

LD_LIBRARY_PATH=/home/work/local/mysql5/lib/mysql

30 12 * * * source ~/.bashrc && cd /home/work/mydir && ./myproj

2) LD_LIBRARY_PATH
Linux运行时有一套共享库(*.so)。共享库的寻找和加载是通过/lib/ld.so (RunTime Shared Library Loader)完成的。ld.so在标准路径(/lib, /usr/lib)下寻找共享库。可是如果第三方库并非安装在标准路径下，程序运行的时候就会出现无法找到库的错误，类似于下面这个报错
ld.so.1: curl: fatal: libgcc_s.so.1: open failed: No such file or directory

LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:/home/work/local/mysql5/lib/mysql
export LD_LIBRARY_PATH

LD_LIBRARY_PATH的缺点：（参考http://xahlee.org/UnixResource_dir/_/ldpath.html）
"For security reasons, LD_LIBRARY_PATH is ignored at runtime for executables that have their setuid or setgid bit set. This severely limits the usefulness of LD_LIBRARY_PATH." ... .... ....."LD_LIBRARY_PATH is one of those insidious things that once it gets set globally for a user, things tend to happen which cause people to rely on it being set. Eventually when LD_LIBRARY_PATH needs to be changed or removed, mass breakage will occur!" ... ... ......"Nowadays you specify the run-time path for an executable at link stage with the -R (or sometimes -rpath) flag to ld. There's also LD_RUN_PATH which is an environment variable which acts to ld just like specifying -R. Before all this you had only -L, which applied not only during compile-time, but during run time as well. There was no way to say “use this directory during compile time” but “use this other directory at run time”. There were some rather spectacular failure modes that one could get in to because of this. "

1) Never ever set LD_LIBRARY_PATH globally.
If you must ship binaries that use shared libraries and want to allow your clients to install the program outside a 'standard' location, do one of the following:
Ship your binaries as .o files, and as part of the install process relink them with the correct installation library path.
Ship executables with a very long “dummy” run-time library path, and as part of the install process use a binary editor to substitute the correct install library path in the executable.
2) If you are forced to set LD_LIBRARY_PATH, do so only as part of a wrapper.
3). Remove the link-time aspect of LD_LIBRARY_PATH.....It would be much cleaner if LD_LIBRARY_PATH only had influence at run-time. If necessary, invent some other environment variable for the job (LD_LINK_PATH).

3) ld.so.conf

"Some OS's (e.g. Linux) have a configurable loader. You can configure what run-time paths to look in by modifying /etc/ld.so.conf. This is almost as bad a LD_LIBRARY_PATH! Install scripts should never modify this file! This file should contain only the standard library locations as shipped with the OS. "

###################

. /etc/profile

. ~/.bash_profile

##################

posted @ 2013-08-28 20:29  596448745  阅读(18870)  评论(1编辑  收藏