LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-21-2012, 03:16 PM   #1
PaulB0
LQ Newbie
 
Registered: Jun 2012
Location: Linden, Virginia, USA
Distribution: Ubuntu 10.04 and Slackware-current
Posts: 3

Rep: Reputation: Disabled
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.
 
Old 06-21-2012, 03:48 PM   #2
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
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
 
1 members found this post helpful.
Old 06-21-2012, 04:22 PM   #3
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,652
Blog Entries: 10

Rep: Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075
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?

Last edited by Habitual; 06-21-2012 at 04:24 PM.
 
Old 06-21-2012, 08:55 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,247

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
'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.
 
Old 06-22-2012, 01:30 PM   #5
PaulB0
LQ Newbie
 
Registered: Jun 2012
Location: Linden, Virginia, USA
Distribution: Ubuntu 10.04 and Slackware-current
Posts: 3

Original Poster
Rep: Reputation: Disabled
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).
 
Old 06-23-2012, 03:06 AM   #6
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cron job is not recognizing environment variables Jon Blanchard Linux - Newbie 6 02-17-2012 10:50 PM
Trouble setting environment variable exitsfunnel Linux - Software 4 10-06-2004 07:49 AM
Cron: TERM environment variable not set. hydro Linux - General 4 04-15-2004 11:59 PM
problem in cron job setting gauravdream Linux - General 1 02-23-2004 01:08 AM
need help setting up a cron job !! J.I.L. Linux - Software 1 10-19-2003 09:54 PM


All times are GMT -5. The time now is 10:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration