当由cron运行时,shell脚本中使用的环境变量在日志文件中显示为空白 [英] Environment variable used in shell script appear blank in log file when run by cron

查看:86
本文介绍了当由cron运行时,shell脚本中使用的环境变量在日志文件中显示为空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅我的无知,但是我对Shell脚本和Linux中环境变量的使用有些陌生.

Forgive my ignorance, but I am somewhat new to shell scripting and the use of environment variables in Linux.

我有一个执行MySQL数据库备份的脚本.我在脚本中有以下几行用于记录数据库的成功转储:

I have a script that performs a backup of a MySQL database. I have the following lines in the script that are used to log a successful dump of the database:

output=`date; echo "Database export successful from $ENV_HOSTNAME to $BACKUP_HOSTNAME"`
echo $output >> /var/log/errorLog

我在/etc/profile中定义了变量(ENV_HOSTNAME和BACKUP_HOSTNAME),如下所示:

I have the variables (ENV_HOSTNAME and BACKUP_HOSTNAME) defined in /etc/profile as follows:

export ENV_HOSTNAME="env1.somename.com"
export BACKUP_HOSTNAME="env2.somename.com"

当我调用printenv时,我看到这些变量已列出,并且当我运行此脚本(以root用户身份)时,变量值正确显示在日志文件中.但是,当我将此脚本设置为使用cron运行时,变量值未出现在日志文件中.我知道该脚本可以运行,因为我可以看到日期和字符串的其余部分,但是变量为空:

I can see these variables listed when I call printenv and the variable values appear in the log file correctly when I run this script (as root). However, when I set this script to run using cron, the variable values are not appearing in the log file. I know that script runs because I can see the date and the rest of string, but the variables are blank:

Fri Nov 6 22:31:05 EST 2009 Database export successful from to

我确定这里缺少一些简单的东西,所以在此先感谢您的帮助.

I'm sure I am missing something simple here, so thank you in advance for the help.

推荐答案

您遇到的问题是:cron无法运行/etc/profile.

There's your problem: cron doesn't run /etc/profile.

cron仅以极少数的环境设置启动;您需要进行其他任何操作,都必须先进行设置,因此通常cron调用脚本来处理设置,然后执行主要操作.

cron is started with only a tiny number of environment settings; anything else you need, you have to set up yourself, so it's customary to have cron call a script to handle the setup and then the main action.

这篇关于当由cron运行时,shell脚本中使用的环境变量在日志文件中显示为空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆