Operating System - HP-UX
1833387 Members
2992 Online
110052 Solutions
New Discussion

Prevent command from hanging

 
SOLVED
Go to solution
Ryan Clerk
Frequent Advisor

Prevent command from hanging

Hello Experts,

I have a script which gathers a bunch of data and sends it to standard out. Sometimes the script doesn't finish and causes the parent shell to hang. Is there a way to execute this command but to timeout in 30 seconds or so. Most of the time the command completes in about 5 seconds or less.

Thanks, Ryan
4 REPLIES 4
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Prevent command from hanging

Hi Ryan:

A question very much like your came up a few months ago. I simply changed my response to match your 30 seconds value.

--------------------------------------------
While this can be done in the shell by dropping your command into the background and doing a ps to look for the PID, I think a better way is to use Perl. In this case we will set up a signal handler. The idea is that we set an alarm to go off in 30 seconds. We then execute your command. (In my case, I simply did a find to load an array; you would substitute your command within the open statement.) When the command finished, we issue an alarm(0) to cancel the alarm. If all went well the array is loaded with your data and is printed but if the command timed out, the alarm signal handler is called to set an error condition.

The neat thing about the Perl approach is that this is exactly the way one would code it in C
and it's just about as fast.

I have a number of perl scripts which use this idiom so I was able to slap an example together in less time than it took to compose this posting.


----------------------------------------------

Simply substiute the name of your script in the sprintf that builds $s_cmd and you are done. You can adjust the timeout by setting $TIMEOUT.

Regards, Clay




If it ain't broke, I can fix that.
Ryan Clerk
Frequent Advisor

Re: Prevent command from hanging

Hi Clay,

Thanks for the quick reply. I don't want to have to rewrite my shell script in Perl. Isn't there a way to do this in the shell?

Thanks, Ryan
A. Clay Stephenson
Acclaimed Contributor

Re: Prevent command from hanging

Hi again Ryan:

Well, if you did a search (which you probably should have done in the first place) on "timeout" and "command", you can almost certainly find several examples BUT this is my method and I'm sticking with it - and, no, you don't have to rewrite the entire parent script.
Just add something like this to your existing shell script:

timeout.pl > myfile # leave in the foreground
STAT=$?
if [ ${STAT} -eq 0 ]
then
echo "All went well"
else
echo "Command Timed Out or Failed; Status = ${STAT}"
fi

Now that's pretty simple. In fact, it suggests that you might do a version of timeout.pl that could take an optional timeout period and commands from the command line. (If you know a bit of Perl).

Regards, Clay

If it ain't broke, I can fix that.
harry d brown jr
Honored Contributor

Re: Prevent command from hanging


Ryan,

You'd be light years ahead if you DID rewrite your script in perl. perl can run 100's of times faster than ksh scripts, especially any scripts that attempt to do data activities.

live free or die
harry
Live Free or Die