Solved

Conditional IF is not running in the Crontab.

Posted on 2006-06-12
5
580 Views
Last Modified: 2013-12-26
I have a script -on Solaris- which is running pretty fine using the sh command, but when I put it in the crontab, it fails to run the condintinal if.
I have inserted "/usr/bin/ksh" in the begining of my script, but with no progress.
I defined a variable PATH, and export it in my script, but with no progress.
I hope if someone can find me a clue to solve that problem.
0
Comment
Question by:A_HASSAN
  • 2
  • 2
5 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16889123
please post the script, at least the problematic part.
0
 
LVL 38

Expert Comment

by:yuzh
ID: 16890973
make your first line looks like

#!/bin/ksh

and then use full path for the commands

if you need more help, post your script.
0
 

Author Comment

by:A_HASSAN
ID: 16899799
Please note that the script is:
#Defining The system shell
#!/bin/ksh
#Config Files
nodes="/home/Test/conf/nodes"
threshod="/home/Test/conf/threshold"
#Getting the threshold values
cpu_major=`grep "cpu major" $threshod|awk '{print $3}'`
cpu_critical=`grep "cpu critical" $threshod|awk '{print $3}'`
disk_major=`grep "disk major" $threshod|awk '{print $3}'`
disk_critical=`grep "disk critical" $threshod|awk '{print $3}'`
#Formating the time
current_time=`date '+%m/%d/%y %H:%M:%S'`
file_time=`date '+%m%d%y_%H%M%S'`
#Defensive Cleaning
rm /home/Test/temp/*
rm /home/Test/alarms/*
#Temp files
temp_disk="/home/Test/temp/temp_disk"
#Output File
output_file="/home/Test/alarms/system_check"$file_time
touch $output_file
#getting nodes checks
for node in `cat $nodes`
do
#Formatting the node name
echo "Node:"$node
prefix_node=`echo $node|awk '{ print substr($1,1,2)}'`
echo "PrefixNode:"$prefix_node
node_name=`echo $node`
if [ $prefix_node -eq "Te" ];
   then
    {
     node_name="ext_"`echo $node`
echo "node_name:"$node_name
    };
fi
node=`rsh $node_name vmstat 10 10|awk '{print $8}'`
done

The nodes file contains:
prod1
prod2
Test1
Test2

It is a simple script to test the if function in the crontab.
This script is working fine manually, but in the crontag, the mail was checked, and the following was found:

Your "cron" job on Test1
/home/Test/bin/system_check

produced the following output:

/home/Test/temp/*: No such file or directory
Node:prod1
PrefixNode:pr
node_name:ext_prod1
Node:prod2
PrefixNode:pr
node_name:ext_prod2
ext_prod2: unknown host
awk: division by zero
/home/Test/bin/system_check: test: argument expected


from the mail reply, it entered the conditional if statment, even when the condition had not being achieved.
which makes me feel that the if is not working?
 

   What do you think?
0
 
LVL 38

Accepted Solution

by:
yuzh earned 500 total points
ID: 16900072
Modify your script to:

1)
   #!/bin/ksh
   as the first line, need to remove "#Defining The system shell" or put is as the 2nd line. to defined what shell to be used.

2)
   
if [ $prefix_node -eq "Te" ];
   then
    {
     node_name="ext_"`echo $node`
echo "node_name:"$node_name
    };
fi

 change it to:
    if [ "prefix_node" = "Te" ] ; then
         node_name="ext_"`echo $node`
         echo "node_name:"$node_name
    fi

 3)
   change:
   prefix_node=`echo $node|awk '{ print substr($1,1,2)}'`
   To:
   prefix_node=`echo $node | cut -c1-2`

you can add:
   "set -x " as the 2nd line of the script, to debug the script

4)
   add "exit" at the end of the script.

   eg:
   #!/bin/ksh
    set -x
    #scritp body goes to here
    exit
   #End of the script

    modify your cronjob to make it write to a log file, to check what's wrong with
the script.

    eg:
   0,30 * * * * /path-to/yourscript > /tmp/myscript.log 2>&1

   then check the  /tmp/myscript.log  file out.


   
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16900204
please remove the line
  #Defining The system shell
The line
  #!/bin/ksh
has to be the very first line, more particular: #! the very first 2 characters in the file
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

21 Experts available now in Live!

Get 1:1 Help Now