top's exit code indicates error--is this a bug?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting top's exit code indicates error--is this a bug?
# 1  
Old 08-11-2008
top's exit code indicates error--is this a bug?

This single line of code in a sh script file
Code:
top -b -n 1 -U $USER

causes the script to prematurely exit with an exit code of 1 (i.e. an error) if the script is run with the -e option (e.g. if
Code:
set -e

is executed near the top of the script file).

Alternatively, you can execute it like
Code:
top -b -n 1 -U $USER
topExitCode=$?
if [ "$topExitCode" -ne "0" ]; then
	echo "ERROR: exit code == $topExitCode detected"
	exit $topExitCode
fi

and prove that that top line is the offender.

So why is top generating an error exit code?

I am on box whose uname -a returns
Code:
Linux jellybean 2.6.20-15-generic #2 SMP Sun Apr 15 07:36:31 UTC 2007 i686 GNU/Linux

The man pages top(1): tasks - Linux man page indicate the following BUG:
Quote:
The top command calculates Cpu(s) by looking at the change in CPU time values between samples. When you first run it, it has no previous sample to compare to, so these initial values are the percentages since boot. It means you need at least two loops or you have to ignore summary output from the first loop. This is problem for example for batch mode. There is a possible workaround if you define the CPULOOP=1 environment variable. The top command will be run one extra hidden loop for CPU data before standard output.
So, naturally I tried
Code:
top -b -n 2 -U $USER

as well as
Code:
CPULOOP=1
top -b -n 1 -U $USER

but both of those still fail.

Does anyone know what is going on? Should I file a bug report?
# 2  
Old 08-11-2008
What version and on what OS/distribution?

I see the same behaviour on a SLES 9.3 server with procps-3.2.5-1.2, but it seems to behave as expected on RHEL 4.6 with procps-3.2.3-8.9.

I had a look through the current source here and could see no reason why it should exit with code 1 (current CVS version of the package is 3.2.7):

SourceForge.net Repository - [procps] View of /procps/top.c
# 3  
Old 08-11-2008
Quote:
Originally Posted by Annihilannic
What version and on what OS/distribution?
Executing
Code:
top -h

yields
Code:
top: procps version 3.2.7

My original post gave the uname -a result. This is a ubuntu server. Is there some other command that you would have me run?


Quote:
Originally Posted by Annihilannic
I see the same behaviour on a SLES 9.3 server with procps-3.2.5-1.2...
Whew--at least someone else sees it too...
# 4  
Old 08-11-2008
Also occurs on SLES 10.1 with procps-3.2.6-18.7. Yeah, I'd go ahead and report the bug (or obtain the source code and attempt to debug it if you are so inclined).
# 5  
Old 08-12-2008
Incidentally, you may already be aware but as a workaround you can do this to ensure your script carries on its merry way without having to turn off set -e:

Code:
top -b -n 1 -U $USER || true

# 6  
Old 08-12-2008
Quote:
Originally Posted by fabulous2
"There is a possible workaround if you define the CPULOOP=1 environment variable."
So, naturally I tried
Code:
CPULOOP=1
top -b -n 1 -U $USER

What kind of shell lets you set an environment variable like that? With sh you need to export it or set it on the command line.
# 7  
Old 08-12-2008
Quote:
Originally Posted by Annihilannic
Incidentally, you may already be aware but as a workaround you can do this to ensure your script carries on its merry way without having to turn off set -e:

Code:
top -b -n 1 -U $USER || true

Thats clever, and it works! It is what I use now.

I was thinking that there had to be some way to suppress the exit code, thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Can't find the bug in my code - bombing with rename

Hi Folks - I'm encountering an issue: Scenario: We have automated GL data loads utilizing FDMEE. The problem is that some of our Locations could have multiple files. I think we are running into a situation where the script is trying to name the 2 files the same name and it is bombing out.... (8 Replies)
Discussion started by: SIMMS7400
8 Replies

2. Shell Programming and Scripting

Exit code

can anyone tell what the exit status - 137 belongs in unix shell scripting. (3 Replies)
Discussion started by: ramkumar15
3 Replies

3. Programming

Algorthm bug of my code

Hello, This porblem bugged me for some time. It is to merge different files of hundred rows to have a union with the ID as key column (kind of similar to join!) and absence with 0. ID File1 A 1 C 3 D 4 M 6 ID File2 A 5 B 10 C 15 Z 26 ID File3 A 2 B 6 O 20 X 9 I want the output... (9 Replies)
Discussion started by: yifangt
9 Replies

4. UNIX for Advanced & Expert Users

How to Make Sql Plus Exit with an Error Code

Dear all, How to make sqlplus command to exit with an apt error code in bash script, It always returns 0 for me. Thanks (9 Replies)
Discussion started by: vetrivendhan
9 Replies

5. UNIX for Dummies Questions & Answers

Exit TOP command after 1 min

Hi, I am running following command: top -p <psid> | grep used I want to send o/p of above command to some txt file but it do not break as it keeps going. How can I exit 'top -p <psid> | grep used' after certain time like 1 min or 2 mins? (1 Reply)
Discussion started by: sach253
1 Replies

6. Shell Programming and Scripting

Can anyone find a bug in this code?? shell script

I have done a script and IT WORKS JUST PERFECT from command line...but in cron it has problems executing... nawk -F"|" ' { s=substr($104,2,18)} {b ++s} END { for (i in b) print i, b } ' $1 > /path/to/files/TranId_w$2 q=`cat /path/to/files/TranId_w$2 | wc -l` echo $q >... (1 Reply)
Discussion started by: amon
1 Replies

7. UNIX for Dummies Questions & Answers

Where can I find a list of exit codes? (Exit code 64)

I'm receiving an exit code 64 in our batch scheduler (BMC product control-m) executing a PERL script on UX-HP. Can you tell me where I can find a list of exit codes and their meaning. I'm assuming the exit code is from the Unix operating system not PERL. (3 Replies)
Discussion started by: jkuchar747
3 Replies

8. Shell Programming and Scripting

All about exit code

Hi, I am working on Solaris 8 and the "intro" man page says, "Upon termination, each command returns two bytes of status, one supplied by the system and given the cause for termination, and (in the case of 'normal' termination) one supplied by the program. The former byte is 0 for normal... (2 Replies)
Discussion started by: cdin2
2 Replies
Login or Register to Ask a Question