The environment variable used in the shell script appears blank in the log file when cron starts

Forgive my ignorance, but I'm a little new to shell scripting and using environment variables in Linux.

I have a script that backs up a MySQL database. I have the following lines in a script that are used to register a successful database dump:

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

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

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

I can see these variables listed when printenv was called, and the values ​​of the variables appear in the log file correctly when I run this script (as root). However, when I set this script to run using cron, the values ​​of the variables do not appear in the log file. I know the script works because I can see the date and the rest of the string, but the variables are empty:

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

I am sure that I am missing something simple here, so in advance for help.

+3
source share
3 answers

There is your problem: cron does not run / etc / profile.

cron ; , , , , cron script , .

+5

G'day,

cron , env. .

, script, , , cron-, script .

, cron script, .

. my_cronenv.sh

script my_cronenv.sh , cron.

: , , script, , cronjobs , cron-, .

- , env. vars, . , ${ENV_HOSTNAME}, $ENV_HOSTNAME.

, , . , ${ENV_HOSTNAME} _RSV , , $ENV_HOSTNAME_RSV, . , , , , . .

,

+5

cron , /etc/profile .

. /etc/profile

script.

+2

Source: https://habr.com/ru/post/1722230/


All Articles