cron execution

Hi,

Here is the code snippet when executing on the command line it does execute smoothly, but while i place this on cron it doesnt run.

Can anybody suggest where the problem in cron is.?

#!/usr/bin/ksh

home_dir="/export/home/scripts/MonitorScripts"
ThresholdLimit=5

touch $home_dir/cpuLog.txt
touch $home_dir/pid_Log.txt

#execute TOP command and store results in a Text

    top |  awk '\{print $1, $10\}' | grep % >  $home_dir/cpuLog.txt
    perl -pi -e 's/%/ /g' cpuLog.txt
    
    sleep 10
    
    while read BUFFER

    do
            row=\`echo $BUFFER\`
            col1=\`echo $row | cut -f1 -d ' '\`
            col2=\`echo $row | cut -f2 -d ' '\`

            if [ $col2 -ge $ThresholdLimit ]; then
                    
                    /usr/ucb/ps -auuwwx | grep $col1 >> $home\_dir/pid_Log.txt
            fi

    done < cpuLog.txt

    checpid=\`wc -l $home\_dir/pid_Log.txt | awk '\{print $1\}\`

    if [ $checpid -ge 0 ]; then
            mailx -s "Check for CPU process running on HIGH\(above 5%\)" -c "" abc@xyz.com < $home\_dir/pid_Log.txt
                    sleep 3
            rm $home_dir/cpuLog.txt
            rm $home\_dir/pid_Log.txt
    else
            rm $home_dir/cpuLog.txt
            rm $home\_dir/pid_Log.txt
            exit
    fi

exit

Here is the cron that is set to.

          • /export/home/scripts/MonitorScripts/cpuMonit.sh

Please mention the permission for the script here.

here you go..

-rwxrwxrwx 1 abc abc1 1442 Oct 22 05:38 cpuMonit.sh

I'm by no means a Solaris expert, but it looks like you are falling for the "cron error number 1": cronjobs have no environment, as they inherit from init and init is notoriously poor. ;-))

When you log on to a system your environment is set (by $HOME/.profile, $HOME/.kshrc, etc.) and all the jobs you start from there will inherit this environment. If you let cron start a job it will inherit the environment from cron, which itself has inherited its from init - and this is (next to) empty. "Normal" commodities like set PATH-variables, etc. are a not there and chances are the commands you use in your script are simply not found.

bakunin

  1. Are the other scripts configured in Cron running successfully.
  2. This can be related to writing the logs in the script.

bakunin,

You are correct, by default when the user logs inn, there is a profile set inorder to export certain paths like below.

PATH=/usr/j2sdk1.4.2_04/bin:$PATH:.:/opt/vnc:/usr/X/bin

Is there anything more that cron is supposed to have in the PATHH to set?

raman,
Yes, there are other scripts too running.

First I'd like to say that raman1605 is correct: maybe your and the cronjobs rights are different and that the script is able to read/write to/from some logfiles doesn't mean the cron-job is able to do the same. That doesn't *need* to be a problem, but it could very well be one.

I have no idea. The best thing you can do to analyze that is to take a snapshot of your own environment by issuing

env > env.file

and look what stands there. Then put into your script everything from there that you think might be necessary, try it again, if it fails add some more, etc., until your script is running.

It is a good idea, though, to create a "standard-function" which sets your scripts environment. You can use this funtion over and over again for all your scripts and if something is missing there you could modify it and all your scripts will benefit. This could look like:

# save this as /usr/local/lib/ksh/stdenv

if [ -z "$NEVER_USE_THIS_VAR" ] ; then
     unset ENV
     typeset -x PATH='/bin:/usr/bin:/usr/local/bin'
     typeset -x FPATH='/usr/local/lib/ksh'
     # ... many more declarations, whatever you need ....

     NEVER_USE_THIS_VAR="kilroy_was_here"
fi

Now your scripts would start like:

#! /bin/ksh

. /usr/local/lib/ksh/stdenv         # set the standard environment

typeset -i somevar=0                # set some local variables

# ... etc., etc.

exit 0

bakunin