LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-11-2010, 01:27 AM   #1
poojithas
LQ Newbie
 
Registered: Jun 2010
Location: Chennai, India
Distribution: Ubuntu 10.04
Posts: 22

Rep: Reputation: 0
capturing exit code of background job


Hi, I want to capture return code of an background job, below is my code snippet; every time I am getting return code 1 if the program has completed within 4 Seconds. Any problem in the script particularly else part ?

#!/bin/bash
(./prog1; Ret=$?) &

sleep 4

if ps $! > /dev/null
then
kill $!
exit 255
else
echo "return code $Ret"
exit Ret
fi
 
Old 10-11-2010, 05:03 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Is your only purpose to ensure that the process ends after four seconds? Because the gnu coreutils include timeout, which will terminate a program (or send it another signal) after a set amount of time.

Edit: Oh yeah, also, running commands inside of (..) means they run as separate sub-processes, so any variables set within them will not be available to higher levels.

Last edited by David the H.; 10-11-2010 at 05:06 AM.
 
Old 10-11-2010, 07:24 AM   #3
poojithas
LQ Newbie
 
Registered: Jun 2010
Location: Chennai, India
Distribution: Ubuntu 10.04
Posts: 22

Original Poster
Rep: Reputation: 0
Hi David,

my intention is to kill the program if it executes beyond 4 seconds, and if it is completed before 4 seconds, I want to exit the script with the program's (prog1) return code. I'm calling this script in C language using system() function and I capture the return code from the system call.

I'm new to shell scripting , please help me how do I achieve this task. Thanks in advance.
 
Old 10-11-2010, 11:15 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
I think you need to provide a bit more detail about the script and what it's supposed to do. Is there more to it than what you've shown above? Does it have to run as a background process? Using them inside scripts can be a real pain to deal with.

Since the program only runs for a few seconds, and you have to wait for it to complete anyway, why not simply run it in the foreground and capture the exit code normally?
 
Old 10-11-2010, 11:54 AM   #5
poojithas
LQ Newbie
 
Registered: Jun 2010
Location: Chennai, India
Distribution: Ubuntu 10.04
Posts: 22

Original Poster
Rep: Reputation: 0
I dynamically build this script in C program:

1. building the script in a file say bashfile.sh
2. run it with below code in C program

Return_code = system("bashfile.sh");
Return_code = Return_code >> 8;
if ( Return_code == 0 ) ..... do some process
if ( Return_code > 0 && Return_code < 255) ..... do some process
if ( Return_code == 255 ) ..... do some process (this means prog1 has been killed)

Note: The script I pasted in my posting is the exact script and there is nothing else in bashfile.sh

why I run it as background: I want to kill the process if it goes into never ending loop.

Expected behavior:
If prog1 normally terminates; I should catch the respective return code in C program
If prog1 goes into never ending loop beyond some time limit; I should be able to cancel it and catch the return code 255

I hope I made it clear, please propose your kind solution.
 
Old 10-11-2010, 03:23 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
It sounds like timeout is what you need then, assuming it's ok for the script to call on external tools (as I mentioned, timeout is part of the gnu coreutils, so all linux systems should have it).

If timeout has to terminate the process it's watching, it exits with a status of 124. If not, it exits with the status of the command.

So simply test the output of timeout, and use it to exit the script with the value you want.
Code:
#!/bin/bash

timeout 4s program

case $? in
     124) exit 255 ;;
       *) exit $?  ;;
esac
timeout also includes options to send stronger kill signals if the standard termination call fails.
 
Old 10-11-2010, 08:20 PM   #7
poojithas
LQ Newbie
 
Registered: Jun 2010
Location: Chennai, India
Distribution: Ubuntu 10.04
Posts: 22

Original Poster
Rep: Reputation: 0
Thats great , it works. thanks a lot David.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Capturing the exit status of the script running in background paragkalra Programming 7 04-11-2010 01:00 PM
capturing data from IP port in background using telnet or netcat (nc) kmslick Linux - Software 4 03-05-2010 01:12 PM
Capturing output from background FTP command Lazypete Linux - Server 6 05-22-2008 10:41 AM
Perl: Running Command line apps in background and capturing output s0l1dsnak3123 Programming 8 03-28-2008 01:24 PM
Background job won't stay in the background! JMJ_coder Linux - General 1 03-17-2008 06:05 PM


All times are GMT -5. The time now is 05:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration