Need help with shell script - output of top command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with shell script - output of top command
# 1  
Old 07-13-2010
Need help with shell script - output of top command

I have written shell script to send file as an attachemt of email and output of "top -o res" command as email body. it works fine if i execute manually from prompt but it does not send "top -o res" command output in email body when it is executed via crontab. Any suggestions. My script is below:
---------------
Code:
#!/usr/bin/ksh -x
 
FILE="/export/local/user/myfolder"
#### null topcommand.log file ####
cat /dev/null > topcommand.log

top -o res >> topcommand.log
generate_email() {
/usr/lib/sendmail -t <<EOF
To: my@testemail.com
From: `$hostname`
Subject: ALERT : Test Subnect Line
EMAILMESSAGE: `cat topcommand.log`
`uuencode ${FILE}/testfile.out testfile.out`
EOF
}
generate_email

------------------

Thanks

---------- Post updated at 12:41 PM ---------- Previous update was at 12:10 PM ----------

Anyone? I would be surprised if no one has done this before? I am sure there are experts who can help me to fix my script.

Thanks in advance for any help.

Last edited by Franklin52; 07-13-2010 at 01:52 PM.. Reason: Please use code tags
# 2  
Old 07-13-2010
Hmm. I though top had an option to show something and quit immediately.

Couldn't find it on my top, so, try:

Code:
echo q | top -o res > topcommand.log

I don't know much about your generate_email() function, because I never use sendmail this way...
# 3  
Old 07-13-2010
Thanks for your response.

Command suggested by you will work too. But my problem is when i execute script, it does put output in email body - not when it is executed via crontab.

What is difference when it is executed via crontab and manually?

Thanks
# 4  
Old 07-13-2010
Quote:
Originally Posted by needyourhelp10
Thanks for your response.

Command suggested by you will work too. But my problem is when i execute script, it does put output in email body - not when it is executed via crontab.

What is difference when it is executed via crontab and manually?

Thanks
Oh, sorry. I missed that "little" detail, when i read your question!

Search the forum for Doesn't work with cron (linked is an example)

And there's a great readme here
This User Gave Thanks to Scott For This Post:
# 5  
Old 07-13-2010
Perfect. It works. Thanks for your help.
# 6  
Old 07-13-2010
There are several problems , but the error messages and the shell trace should be in unix mail for the user who owns the cron.

1) The top program expects a terminal and fails when run from cron if $TERM is not set.
Set $TERM in the script to something suitable.
When it is working, the output from "top" will include screen control characters and may need some work to make it readable.
I too don't have the commands to "top" which you are using. Most versions of "top" have a switch to make top only do one iteration then exit.

2) The file "topcommand.log" will be created in the default root directory for cron. Specify the full hierarchial directory name when referring to a file in a cron or issue a "cd" to the appropriate directory.

3) The file "testfile.out" is not written during this script. Though it has a full path name (which is good), does it exist?

4) The "-x" switch to ksh means that the shell trace will appear in unix mail for the user owning the cron. This might be useful for testing, but is unsuitable for a production script.

5) You may need to use "ux2dos" to convert the file format before "uuencode".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Top Command Output is not coming via Cronjob

Dear All, I created a small script to get the CPU, GIS usage etc automatically. However when i run this script manually its working , but when i run through cronjob i am not getting any output. Can anyone please help me on this. I am using SuseLinux. Thank you in advance. #!/bin/sh {... (2 Replies)
Discussion started by: Nitin Kapoor
2 Replies

2. UNIX for Dummies Questions & Answers

Unable to scroll to the top of the output of a command

Hello, I am a beginner, I have currently Solaris 11 on a vmware machine. Whenever I type a command like 'ls' the screen scrolls to the end of the screen. I am unable to see the entire list of directories and files. I have tried ls |page command, but looking out for a better option. (3 Replies)
Discussion started by: farheenprasad
3 Replies

3. Shell Programming and Scripting

Format Top and prstat command output

need help with formatting output of command top and prstat. My requirment is to remove few columns and display remaining . But when i used awk to do that the output seems to be have gone vague, mixing the column values. After using awk to print particular column the output seems to have lost... (1 Reply)
Discussion started by: NarayanaPrakash
1 Replies

4. HP-UX

Formatted TOP command output in file

Hi All, I want generate HP-UX overall system performance report. I tried executing top command and write that out put to file. but am not able to view the report in proper format. I can see report like below in file but i can see properly in terminal. Please suggest how can i get... (2 Replies)
Discussion started by: lravip123
2 Replies

5. UNIX for Advanced & Expert Users

Conflicts in the output of TOP command

Hi All, In the output of TOP command in my unix system, i monitored that some process has utilization more than 100% even some process has 4000% utilisation. Please help me understand how it is possible to show more than 100% utilization. Please see the screenshot below:... (2 Replies)
Discussion started by: anand2308
2 Replies

6. Shell Programming and Scripting

Shell Script to grep output from top command.

Hello, I want a script which would grep details from top command for specific processes. I m not sure of the PID of these processes but i know the names. $ top -c top - 16:41:55 up 160 days, 5:53, 2 users, load average: 9.36, 9.18, 8.98 Tasks: 288 total, 9 running, 279 sleeping, 0... (8 Replies)
Discussion started by: Siddheshk
8 Replies

7. Shell Programming and Scripting

Capturing first output from 'top'-likes command

Is this a stupid code?? top > top.out & sleep 2 kill %1 cat top.out Thanks, (6 Replies)
Discussion started by: Shawn, Lee
6 Replies

8. HP-UX

Sorting top command output in HP_UX 11.11

Hello all, I've been woking on Solaris and Linux (Red Hat) so far but now I've inherited an HP-UX system and having minor issues with syntax...Appreciate if you could help me out here.. 1) I'm trying to sort the output of the top command in HP-UX 11.11 by pressing O (capital O) after typing... (2 Replies)
Discussion started by: luft
2 Replies

9. Shell Programming and Scripting

Extract header from top command output

hi, I want to extract and save the cpu(s) information from top command output, but individual cpu statistics separately on a multi-processor machine. In command line, top will show this statistics when we press the switch "1". any ideas? thanks, meharo (3 Replies)
Discussion started by: meharo
3 Replies

10. Solaris

Monitoring the output of 'top' command on hourly basis.

I need to capture the following data on an hourly basis through cronjob scheduling:- 1. load averages 2. Total no. of processes. 3. CPU state 4. Memory 5. Top 3 process details. All the above information is available through the command 'top'. But here we need to automate the same and... (4 Replies)
Discussion started by: subharai
4 Replies
Login or Register to Ask a Question