Trapping program return code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trapping program return code
# 1  
Old 01-27-2011
Trapping program return code

Hello

I have a program (prog) that accepts a parameter in order to execute some internal loop grabbing memory in each iteration. I'm using top to monitor the memory usage and to produce an output.
Thus I need the program's pid as a parameter to top.
I capture pid using myPID=$!.
I'm also checking if the program returns an error code in order to stop the script's execution.

Now using & myPID always equals to 0 even if the program returns an error. Removing & error capture works fine but I have no pid.
Is there any work around in order to have error trapping and pid at the same time?

Code:
#!/bin/sh 
./prog $1 &

myPID=$!
if [ $? -ne 0 ]
then
  echo "error captured"
  exit 1
fi

echo $myPID
top -p $myPID -b -d 1 -n $1 | sed -n '8~10p' | awk '{print $5}' > res.txt

# 2  
Old 01-27-2011
Yes, but you cannot access the exit status of the process untill it finishes (sort of obvious really).

So, what you probably want to do is sleep for a second or so and then ensure your program started properly (eg use kill -0 $myPID or check directory /proc/$myPID exists) then do your top command and finally wait $myPID to get it's exist status.
# 3  
Old 01-27-2011
Imho the "-p" parameter to "top" is not the PID.
I can't claim to know every version of "top" but please do check your "man top" and prove me wrong.

In order to suggest a suitable "ps" command we'd need to know what Operating System and version you are running.

Having re-read your post you may need to encapsulate the the process in a script (albeit backgrounded) which checks the exit status of the actual program you are running in background.
In you current script the exit status $? is that of the background command "&". It would be hard to not get a value in $! (background PID) in these circumstances. As ChublerXL implies, check whether the process disappears quickly which would suggest failure.


Important. Backgrounding a task takes real time. On a modern multi-processor system it is prudent to run a short "sleep" command to give the process time to start or fail before monitoring it.

Last edited by methyl; 01-27-2011 at 06:25 PM..
# 4  
Old 01-27-2011
I'm running Ubuntu 10.10, 2.6.35-24-generic kernel.

My code now looks like :

Code:
#!/bin/sh 
./prog $1 &
myPID=$!

sleep 1
kill -0 $myPID

if [ $? -ne  0 ] ; then
  echo 'process terminated'
  exit 
fi

top -p $myPID -b -d 1 -n $1 | sed -n '8~10p' | awk '{print $5}' > res.txt

It runs smoothly Smilie.
Just a slight drawback as prog should iterate $1 times grabbing some memory each time. Between each iteration it sleeps 1 sec. As my script sleeps for 1 sec I monitor $1-1 iterations than $1.
# 5  
Old 01-27-2011
Some sleeps allow you to wait for sub second intervals (e.g. sleep 0.25), not sure Ubuntu 10.10
# 6  
Old 01-28-2011
You are right Chubler_XL. Ubuntu allows float number as sleep time.
Appreciate your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trapping the return status from FIND command

I have the following code in a script: find . \( ! -name . -prune \) -name "cg*" -exec cp -p {} "${temp_dir}" \; ret_stat=$? I think the return status is only captured for the 'find' command and not for the 'cp' command. Is there a way to get the return status for the 'cp' command... (7 Replies)
Discussion started by: vskr72
7 Replies

2. Programming

Segment fault for C++ program when return vector

I am trying to reverse complement DNA sequence (string) with a short c++ code using boost library. Code was compiled without any warning/error, but ran into Segmentation fault. My guess is the function to return a vector, but not sure. #include <iostream> #include <fstream> #include <string>... (14 Replies)
Discussion started by: yifangt
14 Replies

3. Shell Programming and Scripting

How to end the program with a successful return code?

i have the follow shell script that try to find any file in the specified path and upload to oracle. if no file will output in the concurrent log and the program return as error , how do i change the program so that if no file found will give out normal sucessful result. i tried change the exit... (3 Replies)
Discussion started by: feilhk
3 Replies

4. Shell Programming and Scripting

Trapping SIGINT after restarting program.

Tried to add a function to my control_c interrupt here. It works but has one little bug. If the user selects to run the function instead of exiting, the program restarts itself without forking as it should. However, after that control_c no longer works again. I wanted to allow the user to run... (1 Reply)
Discussion started by: Azrael
1 Replies

5. Programming

Parallel Processing Detection and Program Return Value Detection

Hey, for the purpose of a research project I need to know if a specific type of parallel processing is being utilized by any user-run programs. Is there a way to detect whether a program either returns a value to another program at the end of execution, or just utilizes any form of parallel... (4 Replies)
Discussion started by: azar.zorn
4 Replies

6. Shell Programming and Scripting

How to capture C program return values in Kshell

I have a K shell script (ksh) that needs to return an email address. A C program was written (prog1) to now access the email address off of an oracle table. The call to the program in the ksh is prog1 -p parm1 Based on Parm1 the program will read an oracle table and retrieve the email... (2 Replies)
Discussion started by: jclanc8
2 Replies

7. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies

8. Shell Programming and Scripting

How to get return value from C program and logs to file

Hi, I have written scritp which prints output from the executable to standard output as well as in a file. Here "add" is an c executable which returns some value based on inputs. But if tee is not used "$?" returns the return value from add exe. If tee is used it is simply retuning 0. echo... (5 Replies)
Discussion started by: yhacks
5 Replies

9. UNIX for Advanced & Expert Users

Return code from PL/SQL Code

Hi Guys, I was just wondering if anybody can help me with this problem. OK, how we can get a value back from PL/SQL Script (not stored procedure/function) See the below example: (for example aaa.sh) #!/bin/ksh VALUE=`sqlplus -s user/password@test_id <<EOF @xxx.sq EOF` echo $VALUE ... (7 Replies)
Discussion started by: Shaz
7 Replies

10. Shell Programming and Scripting

sql error code trapping

Hello #!bin/ksh sqlplus -s system/manager < |grep '^ORA' |uniq select * from kk; set echo on show spool on end; / EOF save test.sh sh test.sh results ORA-00942: table or view does not exist (3 Replies)
Discussion started by: xiamin
3 Replies
Login or Register to Ask a Question