Solved

Conditional IF is not running in the Crontab.

Posted on 2006-06-12
5
575 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

14 Experts available now in Live!

Get 1:1 Help Now