cron execution


 
Thread Tools Search this Thread
Operating Systems Solaris cron execution
# 1  
Old 10-22-2007
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
# 2  
Old 10-22-2007
Mention permissions.

Please mention the permission for the script here.
# 3  
Old 10-22-2007
here you go..

-rwxrwxrwx 1 abc abc1 1442 Oct 22 05:38 cpuMonit.sh
# 4  
Old 10-22-2007
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
# 5  
Old 10-22-2007
Other Scripts

1. Are the other scripts configured in Cron running successfully.
2. This can be related to writing the logs in the script.
# 6  
Old 10-22-2007
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.
# 7  
Old 10-22-2007
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.

Quote:
Originally Posted by rameek20
Is there anything more that cron is supposed to have in the PATH to set?
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:

Code:
# 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:

Code:
#! /bin/ksh

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

typeset -i somevar=0                # set some local variables

# ... etc., etc.

exit 0

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Execution problem with Cron

Hi, I'm unable to understand why the grep command in cronjob works intermittently Expected output: Grep command to look for a particular string with today's date and exit from infinite while loop else it must sleep for 5 mins I'm using the following statement in my shell script, the... (9 Replies)
Discussion started by: charlie87
9 Replies

2. UNIX for Dummies Questions & Answers

Execution problem with Cron: Script works manually but not w/Cron. Why?

Hello gurus, I am making what I think is a simple db2 call from within a shell script but I am having difficulty producing the desired report when I run the script shown below from a shell script in cron. For example, my script and the crontab file setup is shown below: #!/bin/ksh db2... (3 Replies)
Discussion started by: okonita
3 Replies

3. Shell Programming and Scripting

To stop execution in cron

hi guys i have a question my cron should start executing minute but it sould stop execute only i have tried tis 10 * * * * /home/sample.sh >> /data/band/cron_$(date+|%Y|m|d).log (2 Replies)
Discussion started by: azherkn3
2 Replies

4. UNIX for Dummies Questions & Answers

Execution Problems with Cron

Good evening, ive got this cron to be run: if i run this manually it doesnt work,it takes me to the prompt again /export/app/CO/opge/scr/Informe_parametros_colombia.ksh >/dev/null 2>&1 here is the code fragment: coopge@coopge: opge PRODUCCION>more... (1 Reply)
Discussion started by: alexcol
1 Replies

5. Shell Programming and Scripting

Execution problems with cron

I am new to creating crontab file , i just wrote below (40 19 * 3 * /root/maths/practisecron.sh), the script just prints "Hi". When ever i save the above file i am getting this . # crontab -e crontab: installing new crontab Can you please check where it went wrong.. (1 Reply)
Discussion started by: vkiyv05
1 Replies

6. Shell Programming and Scripting

Execution Problems with Cron

Hi, I have written a shell script to transfer files to a SFTP server passing the filername, source and dest directory as parameters and it runs well. :) I want to schedule this script to run periodically using a cron job. root@pingu # cat /etc/crontab SHELL=/bin/bash... (1 Reply)
Discussion started by: chetancrsp18
1 Replies

7. AIX

Execution Problem with Cron

Guys, I am beginer in unix. There is a cobol file with fixed-width. I want to read the total Line, Word and character count. I have tried with wc-l unix command, but it returns '0'. Please advice me the correct command/steps to get the record count. Thanks in advance. (4 Replies)
Discussion started by: vibhar
4 Replies

8. UNIX and Linux Applications

Execution Problems with Cron

Hi all!! I have a nerve-wracking concept (probably for me!!) which is not understood. My crontab entry looks this way. 33 09 22 3 * /home/myexp.sh "Bgp4 ALL" >/dev/null 2>&1 But cron gets started occasionally. Sometimes it does. Sometimes it does not. And sometimes it hangs in the middle (I... (1 Reply)
Discussion started by: dhivyasuresh
1 Replies

9. Programming

execution problems with cron

how to store a date into file? and how we can access date from the file? ---------- Post updated at 06:09 AM ---------- Previous update was at 06:08 AM ---------- how we can store date in file? (1 Reply)
Discussion started by: causalmodi777
1 Replies

10. HP-UX

Manual vs Cron execution in HP-UX

Below is a simple script that will find the path were the perl command reside. ========================= #!/usr/bin/sh nPerl=`which perl 2>>/dev/null|grep -v "no perl"` if ; then echo "No Perl application exist..." else echo "Existing at $nPerl" fi ========================= When I... (2 Replies)
Discussion started by: padi
2 Replies
Login or Register to Ask a Question