Solved

Conditional IF is not running in the Crontab.

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a Shell script to start a service checking the port 6 50
either24  challenge 19 99
firstChar challenge 13 115
Bartender label printing - switch on and off graphics 3 42
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…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

825 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