Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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?
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.