You are not logged in.

#1 2012-08-28 04:02:31

OakRaider4Life
Member
Registered: 2012-02-08
Posts: 98

Setting an environment variable in cron

I'll keep this post short and sweet.

I have a script that I use to load the date under a variable named $date.
Obviously, I would love to be able to simple use cron to update this variable at midnight every day.

The variables are set by a script named "datevars" which is stored in /usr/local/bin (erego it is in my PATH)
Here's what cron commands did NOT work.

". datevars"
. datevars
export datevars
[EDIT: source /usr/local/bin/datevars]

Any suggestions?

Last edited by OakRaider4Life (2012-08-28 04:05:36)

Offline

#2 2012-08-28 04:05:13

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: Setting an environment variable in cron

Is the script executable?

Offline

#3 2012-08-28 04:06:42

OakRaider4Life
Member
Registered: 2012-02-08
Posts: 98

Re: Setting an environment variable in cron

The script executes just fine when I execute it manually using the command . datevars

It's fully executable with 755 permissions

Offline

#4 2012-08-28 04:17:03

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: Setting an environment variable in cron

how about trying to actually type out 'source' rather than just using the '.'

Offline

#5 2012-08-28 04:17:36

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: Setting an environment variable in cron

nevermind I see the edit

Offline

#6 2012-08-28 04:21:30

Xyne
Forum Fellow
Registered: 2008-08-03
Posts: 6,965
Website

Re: Setting an environment variable in cron

I doubt that setting a date variable with a cronjob is the right approach, but I don't really get what you're trying to do. If you explain what you want to accomplish, maybe we can suggest a better way.

If the variables are set in datevars then sourcing the file will make them visible to the script that sources it, but only to that script. Cron will run that script and then exit, after which point the variable scope disappears.

What you probably want to do is use cron to create a file with the values of the variables from datevars, and then read that dynamically whenever you need them.

If you need the variables to be available to a script/application then either
a) read or source the generated file directly
or
b) write a wrapper script that sources the file before invoking the script/application

Example for b:
foo.sh: generated file using cron and datevars

export foo=bar
export this=that

wrapper script

#!/bin/sh
source /path/to/foo.sh
/path/to/application "$@"

Again, if you provide some details about what you're trying to do I expect that you will find a better solution.


My Arch Linux Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone

Offline

#7 2012-08-28 04:58:47

OakRaider4Life
Member
Registered: 2012-02-08
Posts: 98

Re: Setting an environment variable in cron

My naming convention for directories and archives which involve backing up certain files and staging updates for applications in /opt involves including the day's date. Just to make my life a tad bit easier, I figured I could use a cron job to automatically contain the day's date in a variable which automatically updates itself when the date changes. This way, I can just create these directories using something like mkdir update-"$date" for example, instead of having to type out mkdir update-08-27-2012 myself every time.

These are the contents of the "datevars" script:

#!/bin/bash
export date=`date +%d-%b-%Y`
export day=`date +%d-%b`
export wday=`date +%A-%d-%b`

Offline

#8 2012-08-28 05:19:47

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,362

Re: Setting an environment variable in cron

OakRaider4Life wrote:

My naming convention for directories and archives which involve backing up certain files and staging updates for applications in /opt involves including the day's date. Just to make my life a tad bit easier, I figured I could use a cron job to automatically contain the day's date in a variable which automatically updates itself when the date changes. This way, I can just create these directories using something like mkdir update-"$date" for example, instead of having to type out mkdir update-08-27-2012 myself every time.

He's telling you that whatever environment variable you set is only within the scope of the cron script. Try opening two ttys, export TEST=whatevr in one, you'll notice TEST doesn't appear in the other's environment.

It may be easier to just script the whole call to mkdir (so instead of typing mkdir update-$date you can just call my_script_name).


Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

#9 2012-08-28 05:58:07

Xyne
Forum Fellow
Registered: 2008-08-03
Posts: 6,965
Website

Re: Setting an environment variable in cron

ngoonee wrote:

It may be easier to just script the whole call to mkdir (so instead of typing mkdir update-$date you can just call my_script_name).

+1

You can use

mkdir update-$(date +%d-%b-%Y)

If you're running the commands yourself, create an alias in bashrc:

alias mkdir_update='mkdir update-$(date +%d-%b-%Y)'

If you're using these variables in a script, initialize them at the beginning if they're used multiple times in the script, otherwise just replace them with "$(date +...)".

I see no good reason to use a cronjob to save the output of 3 different calls to date. It's overly complicated and prone to errors.


My Arch Linux Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone

Offline

#10 2012-08-28 23:28:54

OakRaider4Life
Member
Registered: 2012-02-08
Posts: 98

Re: Setting an environment variable in cron

Thanks for the suggestions! I'll look into using one of these alternate methods.

Offline

#11 2012-08-29 00:08:42

Morrad
Member
Registered: 2012-01-18
Posts: 54

Re: Setting an environment variable in cron

Edit: Finished reading the rest of the thread comments, I added nothing, ignore me.

Last edited by Morrad (2012-08-29 00:10:41)

Offline

Board footer

Powered by FluxBB