crontab中使用环境变量的方法
2016-01-20 09:57
357 查看
http://blog.chinaunix.net/uid-21854925-id-106128.html
在crontab中如何引用已定义的环境变量
一个shell脚本 /root/test/shell/test_crontab_env.sh,内部使用了一个环境变量 TEST_CRONTAB_ENV=test_crontab_env
且TEST_CRONTAB_ENV存在于 /etc/profile 中
如:
#!/bin/bash
echo "`date +%Y-%m-%d" "%H:%M:%S` $TEST_CRONTAB_ENV" >> /tmp/test_crontab_env.file
手动执行, 运行结果:
2010-09-04 11:23:38 test_crontab_env
添加到 crontab 中如下
*/1 * * * * /root/test/shell/test_crontab_env.sh
结果:
$> cat /tmp/test_crontab_env.file
2010-09-04 11:25:01
2010-09-04 11:26:01
...
这里引用的环境变量无效了.
使环境变量在crontab中生效的方法:
1.传参的方式
crontab中
*/1 * * * * /root/test/shell/test_crontab_env.sh "test_crontab_env"
test_crontab_env.sh 中
#!/bin/bash
echo "`date +%Y-%m-%d" "%H:%M:%S` $1" >> /tmp/test_crontab_env.file
2.在该shell脚本中定义环境变量
test_crontab_env.sh 中
#!/bin/bash
TEST_CRONTAB_ENV=test_crontab_env
echo "`date +%Y-%m-%d" "%H:%M:%S` $TEST_CRONTAB_ENV" >> /tmp/test_crontab_env.file
3.在该shell脚本中加载环境变量文件
#!/bin/bash
source /etc/profile
echo "`date +%Y-%m-%d" "%H:%M:%S` $TEST_CRONTAB_ENV" >> /tmp/test_crontab_env.file
个人觉得还是第三种方法最实用
------------- end -------------
From: GS
-------------------------------
在crontab中如何引用已定义的环境变量
一个shell脚本 /root/test/shell/test_crontab_env.sh,内部使用了一个环境变量 TEST_CRONTAB_ENV=test_crontab_env
且TEST_CRONTAB_ENV存在于 /etc/profile 中
如:
#!/bin/bash
echo "`date +%Y-%m-%d" "%H:%M:%S` $TEST_CRONTAB_ENV" >> /tmp/test_crontab_env.file
手动执行, 运行结果:
2010-09-04 11:23:38 test_crontab_env
添加到 crontab 中如下
*/1 * * * * /root/test/shell/test_crontab_env.sh
结果:
$> cat /tmp/test_crontab_env.file
2010-09-04 11:25:01
2010-09-04 11:26:01
...
这里引用的环境变量无效了.
使环境变量在crontab中生效的方法:
1.传参的方式
crontab中
*/1 * * * * /root/test/shell/test_crontab_env.sh "test_crontab_env"
test_crontab_env.sh 中
#!/bin/bash
echo "`date +%Y-%m-%d" "%H:%M:%S` $1" >> /tmp/test_crontab_env.file
2.在该shell脚本中定义环境变量
test_crontab_env.sh 中
#!/bin/bash
TEST_CRONTAB_ENV=test_crontab_env
echo "`date +%Y-%m-%d" "%H:%M:%S` $TEST_CRONTAB_ENV" >> /tmp/test_crontab_env.file
3.在该shell脚本中加载环境变量文件
#!/bin/bash
source /etc/profile
echo "`date +%Y-%m-%d" "%H:%M:%S` $TEST_CRONTAB_ENV" >> /tmp/test_crontab_env.file
个人觉得还是第三种方法最实用
------------- end -------------
From: GS
-------------------------------
相关文章推荐
- 回调函数及其用法
- 如何利用紫金桥组态软件和关系数据库通讯
- Eclipse 项目改名 文件改名 变量改名 方法
- java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.String
- 【国密算法那点事儿】解读DES和SM4、RSA和SM2及SM3
- Android21_HttpWatch及文件上传
- 面试时绝对不能说的 10 句话
- justep工具使用(配置和简单例子)
- 深入理解rem、px、em
- 修改 默认操作系统
- Android20_自定义ContentProvider
- Android 漂亮的UI控件
- [Winform][C#]获取系统颜色预定义颜色和现有字体集
- 主席树K-th Number
- 自动化运维的三阶段理论
- java多线程之生产者消费者经典问题
- Android19_ContentProvider之ContentResolver
- Poj 2023 Choose Your Own Adventure
- Python脚本后台运行的几种方式
- erlang shell表格数据对齐