Setting environment variables in Cron file

Hi,

In Cron file i'm using username and password hard-coded and now i wann to use environmental veraiables in cron file.

But Could you please guide me how to use these environmental variables in cron file ?

Thanks,
Shyamu.A

No differences, but when you use cron, your env is not same as if you log in. Depends which *nix system you have.

In then cron env the best default idea is to set env.

Easy to test. Make next envtst.sh script and run it and then run it using cron. Locate it ex. dir /tmp.

cat <<EOF > /tmp/envtst.sh
#!/usr/bin/sh
env > /tmp/envtst.sh.tmp
EOF
chmod a+rx /tmp/envtst.sh

And then run crontab -e to edit cronfile and add next line

* * * * *  /tmp/envtst.sh  >> /tmp/envtst.sh.log 2>&1

Wait about minute and look file /tmp/envtst.sh.tmp

That is your env when you use cron. It's not same as login. PATH is something, not enough usually and so on. Usually HOME is.

One method is to execute your .profile

Example cron, if your /tmp/envtst.sh.tmp include HOME or hardcode path

* * * * * cd $HOME; ./myjob.sh >> /tmp/job1.log 2>&1
# * * * * * cd /somedir; ./myjob.sh >> /tmp/job1.log 2>&1

And myjob.sh is something:

#!/usr/bin/someshell   # sh, ksh, bash, ...

# do setup file = set environmnet
. ./my.setup
# or ex.
. ./.profile

cd $HOME
dosome job ...

my.setup

# add PATH, but not wait so much
PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/some/other/bin
# or set  it - overwrite
PATH=/bin:/usr/bin:/usr/local/bin:/some/other/bin
DBNAME=somedb
LOGDIR=/some/other/loc/log
# variables are global
export PATH DBNAME LOGDIR

Ofcource you can create your script always using this idea - not trust env, set it.

Hi ,

I'm unable to understand your answer and extremly sorry if I wronly stated in the question.

Leeme re-write my question what I wann to be..

In my cron file curently Ex:

45 05 * * 0-4 ksh /u/prject/bin/transfer.sh username password ESP8661 >> /u/prject/log/Transfer_ESP8661.LOG 2>&1

Here username and password hard coded. And now i don't want to use like this as evry time whenever i want to change password i have to change all these files.

For that i wann to use like environmental variables instead of hard coded user name and password.

Could you please advise me how can I write in cron file ????

First, you have to edit your transport.sh so that it takes input from environment variable instead of command line argument.

Second, you have to set the environment variable as VAR=value in the cron. But which is again going to be as similar as what u have now in the command line, you will have to edit again and again.

Might be, you will have to let the script take input from somewhere else, such as file or database; that too in sequence !!!

Hi Greek ,

Thanks for information.

I'm going to the second point which suggested as we had already created environmental variables in some other file.(set_db_env.sh)

This file (set_db_env.sh) we are using in other shell script programs also.

but need to import this file into CRON.Could you please let me know how to import set_db_env.sh in CRON
and how to use that.

Please give with exmple for understanding (I'm using KSH).

Thanks,
Shyamu.A