今天遇到一个问题,在终端里运行newLISP脚本,一切正常,移动到crontab中就不行。即便crontab -e命令运行在同一个用户下,还是有问题。
由于newLISP脚本要用到hive和hadoop,估计是环境变量的问题。所以先用newLISP shell运行(env)命令,将当前环境变量记录到文件evn.lsp中。
然后在newLISP脚本中加载之。
下面是env.lsp文件部分内容:
(set ‘local-envs ‘(("LC_PAPER" "en_US.UTF-8") ("LC_ADDRESS" "en_US.UTF-8") ("HOSTNAME" "hadoopMaster.com") ("LC_MONETARY" "en_US.UTF-8") ("SHELL" "/bin/bash") ("TERM" "dumb") ("HADOOP_HOME" "/usr/local/hadoop") ("HISTSIZE" "5000") ("SSH_CLIENT" "123.125.91.21 44582 22") ("LC_NUMERIC" "en_US.UTF-8") ("SQOOP_HOME" "/home/chenshu/sqoop-1.4.4.bin__hadoop-1.0.0") ("SSH_TTY" "/dev/pts/7") ("LC_ALL" "en_US.UTF-8") ("HISTFILESIZE" "5000") ("USER" "chenshu") ("EMACS" "t") ("LS_COLORS" "") ("LC_TELEPHONE" "en_US.UTF-8") ("HBASE_HOME" "/usr/local/hbase") ("TERMCAP" "") ("TMOUT" "3600") ("COLUMNS" "146") ("MAVEN_HOME" "/opt/apache-maven-3.2.1") ("MAIL" "/var/spool/mail/chenshu") ("PATH" "/usr/local/java/bin:/usr/local/hadoop/bin:/usr/local/hive/bin://usr/local/hbase/bin://usr/local/hive/bin:/usr/local/postgres9.1/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/data/shell:/data/tools:/opt/apache-maven-3.2.1/bin/:/home/chenshu/sqoop-1.4.4.bin__hadoop-1.0.0/bin/:/home/chenshu/bin:/opt/apache-maven-3.2.1/bin/:/home/chenshu/sqoop-1.4.4.bin__hadoop-1.0.0/bin/") ("LC_MESSAGES" "en_US.UTF-8")
。。。
还添加了一行代码,可以自动遍历上面的list,设置每一个环境变量。
(dolist (e local-envs) (env (e 0) (e 1)) )
这样,在crontab中运行test.lsp脚本成功:
* * * * * cd /home/chenshu/export; /usr/bin/newlisp ./test.lsp >/dev/null 2>&1
而test.lsp中代码如下:
#!/usr/bin/newlisp (define (write-log msg) (append-file "hive2mysql.log" (string msg "\n")) ) (load "/opt/newlisp_util/env.lsp") (set ‘hive-create-table-statement (exec (format "hive -e ‘show databases‘ 2>&1"))) (write-log hive-create-table-statement) (exit)
输出结果为Hive中的数据库名称。为保密,这里不列出。
crontab中运行newLISP脚本的环境变量设置,布布扣,bubuko.com
时间: 2024-10-22 23:57:43