Solved

Shell script works when executed from command line but not when using at or cron

Posted on 2006-11-13
10
1,297 Views
Last Modified: 2010-07-27
I am adding to an existing backup to tape script to resolve an error that occurs about once a month.
If I execute this script from the command line it works fine but when using at or cron it does not work.
Here is a tail -20 of the logfile I am reading from the script
=======================================
/data6/CNSPROD/arch/arch_CNSPROD_1_49179.arc.Z
/data6/CNSPROD/arch/arch_CNSPROD_1_49181.arc.Z
/data6/CNSPROD/arch/arch_CNSPROD_1_49183.arc.Z
/data6/CNSPROD/arch/arch_CNSPROD_1_49184.arc.Z
/data6/CNSPROD/arch/arch_CNSPROD_1_49189.arc.Z
/data6/CNSPROD/arch/arch_CNSPROD_1_49187.arc.Z
/data6/CNSPROD/arch/arch_CNSPROD_1_49191.arc.Z
/data6/CNSPROD/arch/
/data6/CNSPROD/
/data6/
/data7/
/

save: /  92 GB 03:21:33    137 files
Unmounted device: /dev/rmt/0n
End: 12/11/06 16:26
Unloading tape...
Error 117: Drive comm
Moving tape from slot 82 to slot 12...
Error 18: Source inside drive
=======================================
here is the new part of the script.
I cut the end of the script out for testing.
=======================================
##################################################################
# set -xv
#!/bin/ksh
#
DBA1='bkreynolds48@yahoo.com';export DBA1
PATH=$PATH:/usr/bin:/usr/sbin/:/sbin:/bin:/usr/opt/networker/bin; export PATH
#
LOGFILE=/data7/nsr/logs/sunday_cold_backup.111206; export LOGFILE
LG=/data7/nsr/lg; export LG
TP=`date "+%m-%d-%y"`;export TP
#
##################################################################
#
if  [[ "`tail -1 $LOGFILE`" = "Error 18: Source inside drive " ]];
then
   tail -2 $LOGFILE | /usr/bin/mailx -s "Tape ERRORS" $DBA1                >$LG 2>&1
    /usr/bin/touch /data7/nsr/verified_TPerrors.$TP
        echo TPerrors                                                                           >>$LG 2>&1
    /usr/bin/mailx -s "this is a test" $DBA1  <$LG                                   >>$LG 2>&1
    /usr/bin/touch /data7/nsr/verified_2NDtry.$TP
        echo 2NDtry                                                                             >>$LG 2>&1
   tail -2 $LOGFILE | /usr/bin/mailx -s "Tape ERRORS" $DBA1                >>$LG 2>&1
    /usr/bin/touch /data7/nsr/verified_TPerrors.$TP
    /usr/bin/mailx -s "this is a test 2- RETRY" $DBA1        <$LG              >>$LG 2>&1
#
else
   tail -5 $LOGFILE | /usr/bin/mailx -s "Cold Backup " $DBA1                >>$LG 2>&1
        echo NOerror                                                                          >>$LG 2>&1
   /usr/bin/mailx -s "this is a test 3" $DBA1      <$LG                           >>$LG 2>&1
    /usr/bin/touch /data7/nsr/verified_backup.$TP
fi
#
#
####################### End of Script #################################
0
Comment
Question by:bkreynolds48
[X]
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
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 58

Expert Comment

by:amit_g
ID: 17933917
Use absolute path for every command. Even though you have set path in the script, you should use absolute paths. Do which commandname e.g. which date, which tail and use the path you get in the script.

Who is the owner of cron job? Does that user get an email when the cron job completes/fails?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 17934799
You haven't defined what "does not work" means.

Do you not get an email?
Are the wrong results sent?
Do you get any relevant error messages?
0
 
LVL 38

Assisted Solution

by:yuzh
yuzh earned 300 total points
ID: 17935140
put
set -x

in your script, redirect the script output to a file when you use cron to run it, eg

30 10 * * * /path-to/yourscript >/tmp/output  2>&1

check tmp/output , or post it if you need more help.
0
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!

 
LVL 1

Author Comment

by:bkreynolds48
ID: 17938711
What does not work from cron or at is that the script falls through to "NOerror" when it should see the tape error 18
I have tried using both "sh" and "ksh" shells

If you look at the script above you will see that I already have set -xv in it

Yuzh the is the output from the "tmp 2>&1" file
/data7/nsr/bk.test: [[: not found
0
 
LVL 48

Accepted Solution

by:
Tintin earned 200 total points
ID: 17941376
The error

/data7/nsr/bk.test: [[: not found

would only occur if you tried to run the script under sh rather than ksh, and the reason it runs under /bin/sh is that your she bang #! is incorrect.

You have:

##################################################################
# set -xv
#!/bin/ksh

When it should be

#!/bin/ksh
##################################################################
# set -xv


The #! line *needs* to be the very first line, otherwise it is ignored.  By default, all cronjobs run under /bin/sh
0
 
LVL 6

Expert Comment

by:bpeterse
ID: 17942944
Another point - albeit minor - is that when you're using Korn shell, you can export your variables directly, i.e.

DBA1='bkreynolds48@yahoo.com';export DBA1  

becomes

export DBA1='bkreynolds48@yahoo.com'

0
 
LVL 38

Assisted Solution

by:yuzh
yuzh earned 300 total points
ID: 17943226
For ksh script, the first line always:
#!/bin/ksh

otherwise, you did n't specify what shell to be used for the script, and cron will use its default shell - sh syntax to run the statements in the script.
0
 
LVL 1

Author Comment

by:bkreynolds48
ID: 17946631
I put the set command after the
#!/bin/ksh
but still have the same results........
/data7/nsr/bk.test: [[: not found

Thanks
Bev
0
 
LVL 1

Author Comment

by:bkreynolds48
ID: 17946722
I changed
if  [[ "`tail -1 $LOGFILE`" = "Error 18: Source inside drive " ]];
to
if  [ "`tail -1 $LOGFILE`" = "Error 18: Source inside drive " ];

and it worked.

Thanks
0
 
LVL 48

Expert Comment

by:Tintin
ID: 17949874
Did you ensure

#!/bin/ksh

was the *very* first line?

In your original code, you had

##############################

as the first line, so if you didn't remove that, that will explain why it was still running under /bin/sh
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

626 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