Link to home
Start Free TrialLog in
Avatar of 1817
1817

asked on

Using Input Parameter fiel in Cron

I am trying to use Cron to call a script. The problem is that I want to pass the script an input parameter file. Whenever I use an input file I get errors depending on how I try to direct the file. This is what the crontab file looks like:
      55 * * 3 * /home/test/cron2.scp input.txt

If I put the input paramters in the crontab file it works, but I want to be able to change the parameters easily.

I have tried to direct the file like this:
55 * * 3 * /home/test/cron2.scp /home/input.txt
55 * * 3 * /home/test/cron2.scp @/home/input.txt
55 * * 3 * /home/test/cron2.scp /home/@input.txt
55 * * 3 * /home/test/cron2.scp </home/input.txt
55 * * 3 * /home/test/cron2.scp >/home/input.txt

The errors I get are; If I use '@' to try and signify that it is a file I get a TNS error from Orcale saying that it is an invalid login/password.
 If I use >, < or just the path and file name, Unix uses the whole string as input (pathname & filename) instead of using the contents of the file.
Avatar of tark
tark

 What errors do you get?  What do you mean by 'direct the file'?  Is it possible to post a copy of the script, an example 'input.txt' file?  What Un*x flavor are you using?
Avatar of 1817

ASKER

Edited text of question
Can you post a copy of the script?  It looks like you are trying to pass the 'input.txt' file as a parameter to the script.  If so, is the script able to find the input file?  Are you trying to pass in the _contents_ of the unput file, or the file itself?  If you want to send the _contents_ of the file, then you could try something along the lines of:

55 * * 3 * /home/test/cron2.scp < `cat /home/input.txt`

(No guarantees on the quoting rules!)
If you really want to pass the name of the input file, and have the script read it, then it could be pathing/environment problems.
  So, I guess I need to know whether you want file contents, or the file itself, a look at the script itself (or at least relevant parts), and the flavor of Unix you're using (can be found by doing a 'uname -a' on the command line).  All of these things have a bearing on whether a cron job works correctly.
Thanks!
tark
Avatar of 1817

ASKER

1.I want the file contents.

2. Unix Flavor: SMP_DC.OSx otis 1.1-94c079 dcosx MIServer-ES 2/256 r3000

3. Unix Script:
# cron2.scp -- cron scheduler to run SQR   #
#            files for ProAnalyst          #
#                                          #
############################################

# Assign input args to variables #
db_login="pbmt/pbmt"
path_list="/home/test/"
path_out="/home/phi/"

# Set up the paths required to run SQR reports #
PATH=$PATH:/home/sqrs/sqr_install/ora/workbench/bin:/usr/bin:/usr/ccs/bin:/usr/u
cb:    ;export PATH

# Set up ORACLE environment variables #
ORACLE_HOME="/home/oracle/product/current"              ;export ORACLE_HOME
ORACLE_BASE="/home/oracle"                              ;export ORACLE_BASE
ORACLE_TERM="vt100"                                     ;export ORACLE_TERM
ORACLE_SID="phi1"                                       ;export ORACLE_SID
TWO_TASK="phi1.otis"                                    ;export TWO_TASK
LD_LIBRARY_PATH=${ORACLE_HOME}/lib:${LD_LIBRARY_PATH}   ;export LD_LIBRARY_PATH

# set up SQR environment variables #
SQRDIR="/home/sqrs/sqr_install/ora/workbench/bin"       ;export SQRDIR
SQRTOOLS=$SQRDIR                                        ;export SQRTOOLS
ESQRDIR=$SQRDIR                                         ;export ESQRDIR
ESQREDITOR=vi                                           ;export ESQREDITOR
PATH=$SQRDIR:$PATH                                      ;export PATH
ESQRTEMP=$HOME/temp                                     ;export ESQRTEMP
ESQRSTARTUP=$SQRDIR/defaults.suf                        ;export ESQRSTARTUP
ESQRUSESQR=NO                                           ;export ESQRUSESQR
VVTERM=vt220                                            ;export VVTERM
VVTERMCAP=$SQRDIR/vvtermcap                             ;export VVTERMCAP
SQRFLAGS="-s -e -i/usr/sqr/include/"                    ;export SQLFLAGS

# Execute the SQR command #
sqr $SQRDIR/pbm_reports/test/cron.sqr $db_login $path_list $path_out $db_login

The first three variables (db_login,path_list, path_out) are te variables that I want passed to it from the input file. Right now , as you can see I have them hard coded. I want the contents of the input.txt file passed to it. The contents can be passed when it is run from the command line, but not when run by cron.


Thanks
 I would try a couple of things, if you haven't already.
1) One possibility is to change how you get the contents into the script.
You could make the _first_ line of your script be something like:
#!/bin/sh
  Whatever you make this, it should have the following characteristics: 1. Point to the shell in which you would like the script to be run.  2. Be the first line of the file.  The '#!' portion of this line is sort of a 'magic cookie' - it is not a comment like you would normally think.  Most shells know that if the first line starts with '#!' followed by a path to a shell, then it should invoke the script using that shell, and execute from there.
  Then, in your script, open the 'input.txt' file, and read it's contents (using the commands corresponding to the shell you decide to use) into the appropriate variables.  This would negate the need to pass any parameters in at all.

2) When you say 'contents can be passed when it is run from the command line', it sounds like you are doing something like:
% /home/test/cron2.scp <db_login> <path_list> <path_out>
  Have you tried doing something like:
% /home/test/cron2.scp < `cat input.txt`
  By 'cat'ting the file, and redirecting the output of the 'cat' command, you should get the same results as if you had typed in each parameter separately on the command line.

Please let me know the results of of 2) here - I think that's really what you'd like to do.

Thanks!
tark
Avatar of 1817

ASKER

When I run it from the command line I do it like this:
% /home/test/cron2.scp /home/test/@input.txt

What do you mean when you say 'Open th e input file and read it's contents'. I do not think that I know how to open the file inside the script.
Avatar of 1817

ASKER

When I run it from the command line I do it like this:
% /home/test/cron2.scp /home/test/@input.txt

What do you mean when you say 'Open the input file and read it's contents'. I do not think that I know how to open the file inside the script.
55 * * 3 * "/home/test/cron2.scp >/home/input.txt"
Avatar of 1817

ASKER

Sigma's anwser doesn't work. I get an error saying:
sh (sh): home/test/cron2.scp >/home/input.txt: Not found
1817,
  Have you tried using 'cat' to get the contents of 'input.txt' yet?  I'd make sure it works on the command line, then try it in the crontab entry.  Again, you may have to play around with quoting rules, but I think backticks should do it (depending on your shell).
Command line:
/home/test/cron2.scp < `cat input.txt`
Crontab:
55 * * 3 * /home/test/cron2.scp < `cat /home/input.txt`

still curious,
tark
Avatar of 1817

ASKER

I hace tried the 'cat' with a variety of different quoting tries and Unix still thinks that the whole line is an input, instead of trying to read the contents of the file.
 Ok - back to square one (for me anyway).  You try:
% /home/test/cron2.scp /home/test/@input.txt
on the command line, and it works just fine.
  Presumably you have also tried:
% /home/test/cron2.scp < /home/test/input.txt
and it gives the same results.  Right?  Wrong?
  Then you have tried (on the command line):
% /home/testcron2.scp `cat /home/input.txt`
And that works fine also, right?  Wrong? (Oh, by the way - forgive my being an idiot.  I was telling you to use the '<' operator along with the 'cat', and I'm not sure what I was thinking...  Sorry!)

  So the question is, Why does it not work via cron?  cron uses the Bourne shell, and attempts to run all scripts in it.  When cron executes a command, it supplies a *default* set of environment variables, and runs from there.  I'm wondering how you assign input to your variables.  Is it:
db_login = $1
path_list = $2
path_out = $3

and if you print them out immediately afterword, what do you get if you're running the script via cron?  This _should_ work fine with the crontab entry:
55 * * 3 * /home/test/cron2.scp < /home/input.txt
but you get what error message (again)?
And you also tried running the script with:
#!/bin/sh
as the very first line? (Actually, the path should be the full path to wherever 'sh' is located.)

Thanks,
tark
ASKER CERTIFIED SOLUTION
Avatar of lockhart
lockhart

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial