Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Using Input Parameter fiel in Cron

Posted on 1997-04-30
Medium Priority
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.
Question by:1817
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

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?

Author Comment

ID: 1292716
Edited text of question

Expert Comment

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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

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 #

# Set up the paths required to run SQR reports #
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

# 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.


Expert Comment

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:
  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.


Author Comment

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.

Author Comment

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.

Expert Comment

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

Author Comment

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

Expert Comment

ID: 1292724
  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`
55 * * 3 * /home/test/cron2.scp < `cat /home/input.txt`

still curious,

Author Comment

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.

Expert Comment

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:
as the very first line? (Actually, the path should be the full path to wherever 'sh' is located.)


Accepted Solution

lockhart earned 300 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:

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

# replace "abo:" with you list of options
set -- `getopt abo: $ARGLIST`
if [ $? != 0 ]
  echo $USAGE
  exit 2
for i in $*
  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;;

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.


Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

722 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