Solved

Using Input Parameter fiel in Cron

Posted on 1997-04-30
13
279 Views
Last Modified: 2013-12-26
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.
0
Comment
Question by:1817
13 Comments
 
LVL 1

Expert Comment

by:tark
ID: 1292715
 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?
0
 

Author Comment

by:1817
ID: 1292716
Edited text of question
0
 
LVL 1

Expert Comment

by:tark
ID: 1292717
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
0
 

Author Comment

by:1817
ID: 1292718
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
0
 
LVL 1

Expert Comment

by:tark
ID: 1292719
 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
0
 

Author Comment

by:1817
ID: 1292720
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.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:1817
ID: 1292721
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.
0
 

Expert Comment

by:Sigma031497
ID: 1292722
55 * * 3 * "/home/test/cron2.scp >/home/input.txt"
0
 

Author Comment

by:1817
ID: 1292723
Sigma's anwser doesn't work. I get an error saying:
sh (sh): home/test/cron2.scp >/home/input.txt: Not found
0
 
LVL 1

Expert Comment

by:tark
ID: 1292724
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
0
 

Author Comment

by:1817
ID: 1292725
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.
0
 
LVL 1

Expert Comment

by:tark
ID: 1292726
 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
0
 
LVL 2

Accepted Solution

by:
lockhart earned 100 total points
ID: 1292727
Instead of trying to feed the file on the cron commandline, just get it inside the script. Here is a code byte (partially taken from the getopt man page) that will process options from a file:

#!/bin/sh
# replace "opts" with /home/input.txt
ARGLIST=`cat opts`

# replace "abo:" with you list of options
set -- `getopt abo: $ARGLIST`
if [ $? != 0 ]
then
  echo $USAGE
  exit 2
fi
for i in $*
do
  echo "Processing ARG $i"
  case $i in
    -a | -b)  echo "Found $i"; FLAG=$i; shift;;
    -o)       echo "Found $i, ARG=$2"; OARG=$2; shift 2;;
    --)       echo "DONE"; shift; break;;
  esac
done

You can modify this as you need, and stop struggling with the vagaries of cron which works differently than most unix tools in that it _doesn't_ feed what comes after your command to the command as an argument. Since you have to type the name of your input file into a file somewhere, put it into your script.

Mike
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now