?
Solved

Conditional IF is not running in the Crontab.

Posted on 2006-06-12
5
Medium Priority
?
597 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

800 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