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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
06-21-2012, 03:16 PM
|
#1
|
LQ Newbie
Registered: Jun 2012
Location: Linden, Virginia, USA
Distribution: Ubuntu 10.04 and Slackware-current
Posts: 3
Rep:
|
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.
|
|
|
06-21-2012, 03:48 PM
|
#2
|
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
|
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.
|
06-21-2012, 04:22 PM
|
#3
|
LQ Veteran
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Rep:
|
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
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.
|
|
|
06-21-2012, 08:55 PM
|
#4
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415
|
'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.
|
|
|
06-22-2012, 01:30 PM
|
#5
|
LQ Newbie
Registered: Jun 2012
Location: Linden, Virginia, USA
Distribution: Ubuntu 10.04 and Slackware-current
Posts: 3
Original Poster
Rep:
|
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).
|
|
|
06-23-2012, 03:06 AM
|
#6
|
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
|
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 03:59 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|