Solved

Conditional IF is not running in the Crontab.

Posted on 2006-06-12
5
589 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
[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
  • 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

763 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