Using Input Parameter fiel in Cron

Posted on 1997-04-30
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

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.
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


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

# 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Different colored text in ComboBox without Subclassing 8 60
How to convert MFC::CString to UTF8 wchar_t* 10 201
Change to event 1 111
Increment column based of a FK 8 20
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 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

18 Experts available now in Live!

Get 1:1 Help Now