Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a shell script that exports values of variables when executed. The same values will be used in another script.
How to run this script(test.sh) in cron.

#!/bin/sh
export I="10"
echo $I

I will be using root access for cron. I tried this command :

*/5 * * * * /home/ubuntu/backup/.test.sh

I checked with environment variables, nothing is updated.

share|improve this question
1  
Exporting variables makes them visible to their children not to the parent shell. How is another script called? From within test.sh or from the parent shell? –  Marco Feb 18 '13 at 12:27
    
test.sh is one script that exports the value. test2.sh is another script the uses the value of I stored as environment variable –  Rudra Feb 18 '13 at 12:32
    
It depends on how does test2.sh get called? If its called by test.sh, then that should be ok. Otherwise it will probably not take over these variables. –  mauro.stettler Feb 18 '13 at 12:40

3 Answers 3

Environment variables can only be passed down between a process and a direct process child. It cannot change or export environment variables to different process trees. Further, the exported variable is only available to process again that are direct descendents of the current process, NOT at the same level as the export is done.

share|improve this answer

If there are 2 scripts in picture, then the variable exported in the first one would be visible to the next one. So, if you have a cron entry like

*/5 * * * * ( test1.sh; test2.sh ) > log.txt

And test1.sh as

#!/bin/sh
export I="10"
echo $

Then $I would be visible in test2.sh.

share|improve this answer
    
What? No. Whatever test1.sh does in its environment has no impact on test2.sh. If you wanted that, you'd need to write . test1.sh; . test2.sh. –  Gilles Feb 20 '13 at 20:00
    
hmm. May be I had tried the way you showed. I guess sourcing is then really different that running a program from a PATH directory ? –  mtk Feb 21 '13 at 7:03
    
Yes, sure, sourcing is different. . test1.sh tells the current shell to parse and execute the commands in test1.sh. test1.sh runs the external program test1.sh which may or may not be a shell script. –  Gilles Feb 21 '13 at 16:29

In fact variables aren't shared across scripts in cron. Even if you define an environment variable like it says in the manpage if you have a script that changes that variable it won't really change for the second script. However, you can use a temporary file (you can even create it in memory if you don't want to rewrite a file on disk) to use between scripts.

For examle:

10 * * * * /path/to/script1.sh
20 * * * * /path/to/script2.sh

Contents of script1.sh:

#!/bin/bash
echo "VAR1='VALUE1'" > /dev/shm/cronsharedfile

Contents of script2.sh:

#!/bin/bash
source /dev/shm/cronsharedfile
do_something_with $VAR1

In this case the second script will have the correct value assigned by script1.sh. I used a file kept in shared memory (/dev/shm/cronsharedfile) but you can create a file on disk (/path/to/whateverfile) .

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.