Finding return code for completed process ?? | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Finding return code for completed process ??

UNIX for Dummies Questions & Answers


Tags
background processes, process id, return code

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-09-2013
sriraman2 sriraman2 is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 16 January 2013, 10:04 AM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Finding return code for completed process ??

I have Process Id for one background process and currently it is running state. I want to see the return code after the above running process is done.

for the forground process normally I use "echo $?". But how to get the return code for my background process on UNIX/Linux??

Thanks in advance!

Sriram
Sponsored Links
    #2  
Old 01-09-2013
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 16 July 2014, 11:41 PM EDT
Posts: 3,057
Thanks: 33
Thanked 657 Times in 636 Posts

Code:
 
$ cat k.sh
#!/bin/sh
echo "TEST"
grep test a.txt &
wait $!
echo $? 
 
$ sh k.sh
TEST
grep: can't open a.txt
2
 
$ echo test > a.txt
 
$ sh k.sh
TEST
test
0

Sponsored Links
    #3  
Old 01-10-2013
sriraman2 sriraman2 is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 16 January 2013, 10:04 AM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

Thanks for your input. But my requirement is little bit different.

My running process is not child process of my current process. So it is triggered by some other user and I would like to monitor that process and see the return code. Wait command will work only for child process.

Pls help me...
    #4  
Old 01-10-2013
methyl methyl is offline Forum Advisor  
Advisor
 
Join Date: Mar 2008
Last Activity: 18 April 2014, 5:13 AM EDT
Posts: 6,396
Thanks: 287
Thanked 672 Times in 642 Posts
Within the script which runs the process, record the exit status to a file. Then monitor the file from your monitoring script.
Sponsored Links
    #5  
Old 01-11-2013
sriraman2 sriraman2 is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 16 January 2013, 10:04 AM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Ok, is there any other way I can capture using command?
Sponsored Links
    #6  
Old 01-11-2013
hergp hergp is offline Forum Advisor  
Problem Eliminator
 
Join Date: Jan 2010
Last Activity: 26 July 2014, 3:41 PM EDT
Location: Vienna, Austria
Posts: 789
Thanks: 18
Thanked 167 Times in 149 Posts
If you are on Solaris, you could use dtrace for this. Create a file exit.d as


Code:
#!/usr/sbin/dtrace -s

syscall::rexit:
/execname == "ls"/
{
    printf ("ls exited with exitcode %d\n", arg0);
}

This example catches the exit code of the ls command. When you run the script, you get some output like:


Code:
dtrace: script './exit.d' matched 2 probes
CPU     ID                    FUNCTION:NAME
  7 105722                      rexit:entry ls exited with exitcode 0
  3 105722                      rexit:entry ls exited with exitcode 2
  1 105722                      rexit:entry ls exited with exitcode 0

The script will run, until you stop it with Ctrl-C.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
aix 4.2: finding out the return code of a savevg/mksysb ? Browser_ice AIX 1 05-14-2009 10:46 AM
Return code of background process c19h28O2 Shell Programming and Scripting 3 04-06-2008 11:46 AM
return code of a process filedeliver Programming 1 04-19-2007 01:42 AM
return code of a process filedeliver UNIX for Advanced & Expert Users 1 04-18-2007 03:27 PM
background process return code Vikas Sood Shell Programming and Scripting 5 06-10-2006 09:25 AM



All times are GMT -4. The time now is 05:16 PM.