LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Having trouble setting an environment variable with a cron job (https://www.linuxquestions.org/questions/linux-newbie-8/having-trouble-setting-an-environment-variable-with-a-cron-job-4175412709/)

PaulB0 06-21-2012 03:16 PM

Having trouble setting an environment variable with a cron job
 
I'd like to add a couple of environment variables to crontab (either the system crontab or my user crontab) called $TODAY and $TOMORROW. But when I add a line like this in my crontab:

Code:

00 0 * * *  export TODAY=`date -d today +%F`; export TOMORROW=`date -d tomorrow +%F`
the variables are not updated.

I've also tried running this script from crontab:

Code:

#!/bin/bash
export TODAY=`date -d today +%F`
export TOMORROW=`date -d tomorrow +%F`

but I can't even get that to work when I run it from the command line (although
Code:

export TODAY=`date -d today +%F`; export TOMORROW=`date -d tomorrow +%F`
does work.)

What am I doing wrong? This seems pretty simple, but obviously I'm missing something.

Thanks.

lithos 06-21-2012 03:48 PM

well,
when you run a script from BASH what it does is run another "instance" (parallel shell) of Bash for the user which is running it
and thus it doesn't use the same Vars = it doesn't update what you want.

What is practice in scripting is to define a variable like:
Code:

#!/bin/bash
TODAY=$(date -d today +%F)
TOMORROW=$(date -d tomorrow +%F)


^^^ no export

and these variables are available only when script is running, then they get unset.

If you would like to try set them to the environment, then call the script with:
Code:

# source script_name.sh
which will leave the variables set whenever you run it and then you will have to unset them manually

good luck

Habitual 06-21-2012 04:22 PM

Code:

#!/bin/bash
TODAY=$(date -d today +%F)
TOMORROW=$(date -d tomorrow +%F)

save as /home/$user/name.sh or such.
terminal >
Code:

chmod 700 /home/$user/name.sh
Code:

crontab -e
and add
00 0 * * * /home/$user/name.sh

isn't export only to retain the "$TODAY" AND "$TOMORROW" outside of the script?

chrism01 06-21-2012 08:55 PM

'export' only exports vars/vals to any sub-shells called from the current shell they were defined in.
I think you are doing something odd here...
What exactly are you trying to achieve and why; this will help give a better answer.

PaulB0 06-22-2012 01:30 PM

Here's a little more explanation about what I am doing.

I have a headless server running Ubuntu 10.04.4 LTS that I access via ssh from several different machines. I know I could put the variables that I wish to define in $HOME/.profile or $HOME/.bashrc, but I also use GNU screen, so 99% I'm attaching and detaching the same session day after day. That means (because I'm not launching a new bash session), the definitions in .profile or wherever are evaluated once: when I log on at the beginning of a new screen session. Then they get "stale" because time marches on, but screen is still stuck with the environment it was initialized with.

FWIW, these variables are just little shortcuts for $TODAY and $TOMORROW that save me having to type out `date -d today|tomorrow +%F` every time I need the date (which is often).

lithos 06-23-2012 03:06 AM

why don't you define an ALIAS
Code:

alias today='date -d today +"%F"'
Put this in "bashrc" or what Ububtu stores bash profile in and you're done.


All times are GMT -5. The time now is 04:06 AM.