cancel
Showing results for 
Search instead for 
Did you mean: 

Variables in crontab

wojtek75
Frequent Advisor

Variables in crontab

Hi,

it looks like the following doesn't work:

=====begin crontab======
ORACLE_HOME=/very/long/path/to/oracle/10g/db

1 2 * * * $ORACLE_HOME/scripts/run1.sh
1 2 * * * $ORACLE_HOME/scripts/run2.sh
#[...]
1 2 * * * $ORACLE_HOME/scripts/run1000.sh
=====end crontab======

Does anybody know if there is any way to define my own variables to be used in crontab entries? I wouldn't like to take advantage of external files with variables definitions.
10 REPLIES
Ganesan R
Honored Contributor

Re: Variables in crontab

Hi,

Put the variables inside the script and schedule the script in crontab.

Or use the absolute path in the cron entry.
Best wishes,

Ganesh.
T G Manikandan
Honored Contributor

Re: Variables in crontab

You should embed these variables within your script.

Also,you can create an environmental file to have all the variables and you can call this .env in your script or both like

0 1 * * * (/home/test/.env /home/test/run.sh)
wojtek75
Frequent Advisor

Re: Variables in crontab

Yep, but what if I need a variable in the path to the script? I don't want to use absolute as it is a kind of dynamic one.
James R. Ferguson
Acclaimed Contributor

Re: Variables in crontab

Hi:

If you don't want to code necessary variables in your script and/or you don't want to source (read) another file of variables when you execute your script, you could do:

# cat /tmp/mysh
#!/usr/bin/sh
echo "${WHO} --- ${WHY}"
exit 0

...with a 'crontab' entry like:

* * * * * ( export WHO="me"; export WHY="I said so"; /tmp/mysh )

Regards!

...JRF...
Steven Schweda
Honored Contributor

Re: Variables in crontab

> Yep, but what if I need a variable in the
> path to the script?

Then you may be doomed.

> I don't want to use
> absolute as it is a kind of dynamic one.

Yes, but if cron has a way to get the value
of your variable, then it's a mystery to me.
I'd say that you need to have a script in
some fixed, known location, so that cron can
find it. That script can then do anything
dynamic which you need to do.

Of course, how you'll choose to inform that
script about the dynamic stuff is still an
open question, but many things are possible.
Matti_Kurkela
Honored Contributor

Re: Variables in crontab

So you'll only want to shorten the names of the scripts to make the crontab easier to read?

Use symbolic links.

First make a directory for the links in some convenient path:

mkdir /usr/local/cronscripts

Then link all the scripts to the directory:

ln -s /very/long/path/to/oracle/10g/db/scripts/run*.sh /usr/local/cronscripts

After this, you should be able to setup your crontab entries like this:

1 2 * * * /usr/local/cronscripts/run1.sh
...etc...

Of course, you should keep in mind that the ORACLE_HOME variable is not defined when the scripts are executed, unless the scripts themselves handle that.

MK
MK
OldSchool
Honored Contributor

Re: Variables in crontab

"=====begin crontab======
ORACLE_HOME=/very/long/path/to/oracle/10g/db

1 2 * * * $ORACLE_HOME/scripts/run1.sh
1 2 * * * $ORACLE_HOME/scripts/run2.sh
#[...]
1 2 * * * $ORACLE_HOME/scripts/run1000.sh
=====end crontab======"

that can't be done...

you might consider making a symbolic link, like /oracle_home and point at your long path, so that crontab entries become

/oracle_home/scripts/run1.sh

then, as suggested previously, have your scripts source whatever environment files are necessary.
Bill Hassell
Honored Contributor

Re: Variables in crontab

cron is not like an interactive shell -- everything must be setup in your script. f your scripts bounce around, I hope you have lots of ways to track them, but I would recommend a single script that handles everything. Rather than hardcode the script names in cron, use a top level script with a runtime parameter to specify what to do. This top level script would also take care of exporting the correct variables and setting values such as $PATH .


Bill Hassell, sysadmin
Dennis Handly
Acclaimed Contributor

Re: Variables in crontab

>Does anybody know if there is any way to define my own variables to be used in crontab entries?

You could have a template crontab file and then write a sed script to change your "variables", then submit to crontab.
George Spencer_4
Frequent Advisor

Re: Variables in crontab

Our database group have a number of scripts to set the environment variables for the desired database instance. We can then run these to set up the environment within crontabs; e.g.

43 11 * * * . /home/informix/env/env_xxxx; script_to_run

The dot before the first script name causes initial script to run in the crontab shell; this sets up all of the variables for the second script.