LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   script using "/usr/bin/cat error" produces "cannot open" in cron (http://www.linuxquestions.org/questions/programming-9/script-using-usr-bin-cat-error-produces-cannot-open-in-cron-742011/)

Dcrusoe 07-22-2009 01:10 PM

script using "/usr/bin/cat error" produces "cannot open" in cron
 
Okay, once again I come hat in hand with a dumb issue. I have a script that work from the command line, but error on a cat of a tmp file when run from cron.

script is running #!/bin/ksh

I am using three tmp files which I have set up in the script to rm -rf them before the main script runs
And then just doing a simple
sed 's/^.//g' $TMP2 > $TMP3

this creates the tmp3 file just fine. the file no will contain numbers without the leading zeros like so:
2486
1009
6750
1126
8836
1125
8649
1060
1057
9849
7082
8847

Then after some header crap echoes out to the report I do a

for i in `/usr/bin/cat tmp.3`
do
z=`grep $i /usr/projects/sds/1,5/usr.log`
echo "$i ->\r\n $z" >>$HDRPT
done

This works when I run it using sh -x and runs normally with a ./filename, and with full path to the script at the command line.

However in cron it does not run correctly and the error is regarding the cat command.

cat: 0652-050 Cannot open tmp.3.
cat: 0652-050 Cannot open tmp.3

Two errors as I have the command running twice in the script with two different greps. The headers go to the finale report fine and the tmp files are all correctly updated. So I seem to be over looking an issue with how the cat command can be run on cron. I have as you can see full path but I am also making a call to the user profile as my first command.

Suggestions or is this to little information?

Thanks in advance for any thoughts.

raconteur 07-22-2009 02:28 PM

It is difficult to give an exact solution because we don't have the whole script, but I can make a fair guess that the problem lies in the fact that cron doesn't have a tty or a full user environment. I know you are sourcing a user's profile, but that may not be enough. There are other possibilities; missing path specifications, permission mismatches, some others but more detail would help...

jan61 07-22-2009 03:06 PM

Moin,

check the working directory, when the script runs from cron. May be, there's no write permission in that directory, so the file can't be created. You should use absolute pathnames for your temporary files to make sure, that you create them in a writable path.

Jan

Dcrusoe 07-22-2009 03:21 PM

issue details with script
 
okay whole script below. Note that I have a file showing users logging into a menu and what they do in the menu, I first pull just the help desk guys from it (tmp.1), then I pull just a user id number(tmp.2), they all have a leading zero in it that I then have to get rid of (tmp.3), finally I use tmp.3 to grep out the id number from yet another file. Note also the headers that I show going to the file do go to the file when run in cron. The only thing not working is the cat command. Maybe it's the fact it is in ksh? I am home now but tomorrow I'll try it under sh, though i and my users all use the ksh shell. Anyway I've commented out the cat and replaced it with numbers embedded in the script and it of course runs. I still think it is something with the cat command, but hey what the hell do I know.


<=====================start script====================================>
#Call .profile to setup the environment so script can also be run from cron
. /usr/projects/sds/defs/.sds_profile

check_for_error()
{
#LOOK FOR AN ERROR MESSAGE FROM TRANS-BASIC -OR- A PROGRAM
egrep "\?Syntax error|UNEXPECTED PROGRAM ERROR" $LOGFILE > /dev/null
if [ "$?" = "0" ]
then
echo "\a\c" > /dev/console
date=`date +"%d-%h-%y %H:%M"`
echo "\r\n"$date" ** SDSII:HDUSER.COM Processing ABORTED" > $consol_log_printer
echo "\r\n"$date" ** Check SDSII:HDUSER.LOG for errors\r\n" > $consol_log_printer
exit 1
fi
}

# set and clear temp files
LOGFILE=/usr/projects/sds/1,3/hduser.log
HDRPT=/usr/projects/sds/1,5/hduser.rpt
TMP1=/usr/projects/sds/1,3/tmp.1
TMP2=/usr/projects/sds/1,3/tmp.2
TMP3=/usr/projects/sds/1,3/tmp.3
/bin/rm -rf $LOGFILE
/bin/rm -rf $TMP1
/bin/rm -rf $TMP2
/bin/rm -rf $TMP3

#SEND BATCH FILE STARTUP MESSSAGE TO THE CONSOL LOG PRINTER
date=`date +"%d-%h-%y %H:%M"`
echo "\r\n"$date" ** SDSII:HDUSER.COM Processing In Progress\r\n" > $consol_log_printer

# First is to get an updated list
# We do this by running the emps prgram and letting it print a employee report
# by Job Function

#fmemps
tbasic << !EOF! >> $LOGFILE
RUN SDSII:FMEMPS
4
R
J

!EOF!
check_for_error

# All Helpdesk are listed under "MIS Help Desk"
# so grep them out and throw them to a tmp file.

grep "MIS Help Desk" /usr/projects/sds/1,5/emps.rpt > $TMP1

# But we only need the gaming license number not
# everything else so use sed to grab that field.

awk '$1 {print $1}' $TMP1 > $TMP2

# But the report gives us the license with a leading zero so get rid of that.

sed 's/^.//g' $TMP2 > $TMP3

# Now we can finally produce the report, but is it today info or yesterday's
# info. Throw a header to the final report, first we do today's info, then
# we need to grep the usr.log file as that is todays stuff.

echo " TODAY'S HELPDESK USER LOG" > $HDRPT
echo "\r\n"$date"\r\n" >> $HDRPT
echo " DATE TIME REQUESTED OPTION ACCESS KB LOGICAL EMP ID EMPLOYEE NAME" >> $HDRPT
echo " ------- ----- -------------------------- ------------------ -- ------- ------ --------------------" >> $HDRPT
sleep 1

for i in `/usr/bin/cat tmp.3`
do
z=`grep $i /usr/projects/sds/1,5/usr.log`
echo "$i ->\r\n $z" >>$HDRPT
done

# Now we produce yesterdays info for the report.
# Throw another header to show is yesterday's info, then
# we need to grep the usr.rpt file as that is yesterdays stuff.

echo " YESTERDAY'S HELPDESK USER LOG" >> $HDRPT
echo " DATE TIME REQUESTED OPTION ACCESS KB LOGICAL EMP ID EMPLOYEE NAME" >> $HDRPT
echo " ------- ----- -------------------------- ------------------ -- ------- ------ --------------------" >> $HDRPT

for i in `/usr/bin/cat tmp.3`
do
z=`grep $i /usr/projects/sds/1,5/usr.rpt`
echo "$i ->\r\n$z" >>$HDRPT
done

#SEND BATCH FILE COMPLETED MESSSAGE TO THE CONSOL LOG PRINTER
date=`date +"%d-%h-%y %H:%M"`
echo $date" ** SDSII:HDUSER.COM Processing Completed\r\n" > $consol_log_printer

jan61 07-22-2009 03:26 PM

Moin,

using the "code" tags would be nice ;-) You should use the $TMP3 variable whithin the cat loop instead of "tmp.3", otherwise it works only if your working directory is /usr/projects/sds/1,3

Jan

Dcrusoe 07-22-2009 03:27 PM

Quote:

Originally Posted by jan61 (Post 3616895)
Moin,

using the "code" tags would be nice ;-) You should use the $TMP3 variable whithin the cat loop instead of "tmp.3", otherwise it works only if your working directory is /usr/projects/sds/1,3

Jan

oh crap! yes! bloody hell. I did say dumb right?

Dcrusoe 07-22-2009 03:30 PM

Quote:

Originally Posted by jan61 (Post 3616895)
Moin,

using the "code" tags would be nice ;-) You should use the $TMP3 variable whithin the cat loop instead of "tmp.3", otherwise it works only if your working directory is /usr/projects/sds/1,3

Jan

:o:redface:\ such a dumb miss! Thank you!


All times are GMT -5. The time now is 08:32 AM.