Operating System - HP-UX
1833828 Members
2088 Online
110063 Solutions
New Discussion

top not running from cron entry..............

 
SOLVED
Go to solution
boomer_2
Super Advisor

top not running from cron entry..............

hi guys,
I m not able to run this script from cron,but when i run it from prompt , runs fine...........

# dattop.sh
DT=`/usr/bin/date|awk '{print $1}'`
TIME=`/usr/bin/date|awk '{print $2,$3,$4}'`
case $DT in
Mon )echo $TIME" ------>" `/usr/bin/top -n3 -d1|awk '{printf $61}'` >> /tmp/Monday.txt ;;
Tue )echo $TIME" ------>"`/usr/bin/top -n3 -d1|awk '{printf $61}'` >> /tmp/Tuesday.txt
;;
Wed )echo $TIME" ------>" `/usr/bin/top -n3 -d1|awk '{printf $61}'` >> /tmp/Wednesday.txt
;;
Thu )echo $TIME" ------>"`/usr/bin/top -n3 -d1|awk '{printf $61}'` >> /tmp/Thursday.txt
;;
Fri )echo $TIME" ------>"`/usr/bin/top -n3 -d1|awk '{printf $61}'` >> /tmp/Friday.txt
;;
Sat )echo $TIME" ------>"`/usr/bin/top -n3 -d1|awk '{printf $61}'` >> /tmp/Saturday.txt
;;
Sun )echo $TIME" ------>" `/usr/bin/top -n3 -d1|awk '{printf $61}'` >> /tmp/Sunday.txt
;;
esac


cron entry is as follows:->

5,10,15,20,25,30,35,40,45,50,55 * * * * /tmp/dattop.sh
7 REPLIES 7
AwadheshPandey
Honored Contributor

Re: top not running from cron entry..............

top -n -f outfile
It's kind of fun to do the impossible
Dennis Handly
Acclaimed Contributor

Re: top not running from cron entry..............

You probably need to use the "-f file" option of top(1), you can't use a pipe, and $61 won't be right.
Steven E. Protter
Exalted Contributor

Re: top not running from cron entry..............

Shalom,

Since cron has limited or no information on TERM/terminal information, your command will fail without the -f parameter directing the output to go to a file.

It is possible to set up environment in the script /tmp/dattop.sh

Also note that many sysadmins set up cron jobs that delete files that are older than a certain number of days in the /tmp filesystem to prevent congestion(full fs). This is probably not a great place to leave a production script.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
boomer_2
Super Advisor

Re: top not running from cron entry..............

hi steven,
then how do i set my environment for this kind of script...
Plzz let me know...
Also denis why i cant use pipe and awk as the script runs fine in runtime.???
Dennis Handly
Acclaimed Contributor
Solution

Re: top not running from cron entry..............

>why i cant use pipe and awk as the script runs fine in runtime???

You might be able to export TERM in your script and get it to work the same.
But you should not be trying to parse the top screen output. It is better to use the "-f file" output.

top -f doesn't support output to a unnamed pipe, and the "-" convention doesn't work.

So you would send the output to a file, use awk on it (this part will change since you have multiple lines), then remove the file.
Steven E. Protter
Exalted Contributor

Re: top not running from cron entry..............

Honestly I don't think you need to set the environment, just use the -f parameter.

But if you want:

From the command line:

env > /tmp/env.norm.txt

Take what you need from there and past it into the script that cron is running.

PATH and TERM are critical. stty -a those settings may matter as well.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
boomer_2
Super Advisor

Re: top not running from cron entry..............

Thank u STEVEN,DENIS i followed ur advice and getting d required o/p from cron now successfully.........thanks again..
POINTS GIVEN