1820694 Members
2612 Online
109627 Solutions
New Discussion юеВ

Re: Command Timeout

 
SOLVED
Go to solution
Greg White
Frequent Advisor

Command Timeout

Hi Experts:

I have an interesting problem. I need to run a command which lists a series of status messages to standard output. These messages may be only a few lines or hundreds of lines but I need it to timeout after 10 seconds. Is there a way to do this without having to write a C program? The command which gathers the information runs fines. I just can't make it timeout and not hang the parent script. Any ideas?

Thanks, Greg
I know how to do it in pascal.
11 REPLIES 11
Steven Sim Kok Leong
Honored Contributor

Re: Command Timeout

Hi,

Have a script that spawns off this program eg.

monitor.sh:
=============================
#!/sbin/sh

10_sec_script &
sleep 10
kill `ps -fae|grep -v grep|grep " 10_sec_script "
=============================
Hope this helps. Regards.

Steven Sim Kok Leong
Brainbench MVP for Unix Admin
http://www.brainbench.com
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Command Timeout

Hi Greg:

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 10 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.

Enjoy, Clay
If it ain't broke, I can fix that.
Greg White
Frequent Advisor

Re: Command Timeout

Thanks Guys!

Steven, I guess the script approach you did was what Clay suggested. It works but I really like Clay's perl scrpt. This is just like I would do it in C. I guess I'm going to have to learn Perl.

Thanks to both of you!

Greg
I know how to do it in pascal.
A. Clay Stephenson
Acclaimed Contributor

Re: Command Timeout

Hi Again Greg:

Yes, Steven's script was close to what I alluded to although I would have used $! (the pid of the last background command). $! is more reliable than the ps -e grep | grep -v grep in that your command might have had grep in it. The concept is nonetheless the same; I simply consider the Perl approach more elegant and it is now my weapon of choice. If you know C, Perl is pretty easy. Things like sockets code very much the same. Note also that I loaded an entire array with one statement!. The other interesting statement is the chomp statement. It removes the LF's from the entire array. chomp(@arry1) processes thbe entire array while chomp($arry1[0]) processes only the first element. Pretty neat stuff!

Regards, Clay
If it ain't broke, I can fix that.
Steven Sim Kok Leong
Honored Contributor

Re: Command Timeout

Hi,

Clay's approach is different, neater and cleaner, dealing with signal handling directly using perl scripting.

Mine is the "quick and dirty, but works" approach using plain OS utilities and shell scripting. :)

Hope this helps. Regards.

Steven Sim Kok Leong
Brainbench MVP for Unix Admin
http://www.brainbench.com
Steven Sim Kok Leong
Honored Contributor

Re: Command Timeout

Hi,

Oops :P Missed out the awk part.

It should be:

kill `ps -fae|grep " 10_sec_script "|grep -v grep|awk '{print $2}'`

Hope this helps. Regards.

Steven Sim Kok Leong
Brainbench MVP for Unix Admin
http://www.brainbench.com
Greg White
Frequent Advisor

Re: Command Timeout

Hi Guys:

I have another question. I changed Clay's Perl script to capture my output and it works great! Can I also capture standard error from the same command? It can be in the same output I just need to get both standard output and standard error from my command's output.

Thanks again, Greg.
I know how to do it in pascal.
A. Clay Stephenson
Acclaimed Contributor

Re: Command Timeout

Hi Greg:

You are going to shoot yourself because it's so easy. All you need to do is add 2>&1 just before the '|' in the open statement. I've attached the script so that you can see the exact syntax. I also changed the script to build the command in an sprintf statement since you might need to add command line args 'on the fly'. One other minor change is that I added a clause to also trap the case where your command itself fails rather than simply timing out.

Regards, Clay

If it ain't broke, I can fix that.

Re: Command Timeout

Just add the following to the end of your commands output:

2>&1

This tells the shell to sened stderr to the same file descriptor as stdout

HTH

Duncan

I am an HPE Employee
Accept or Kudo
Greg White
Frequent Advisor

Re: Command Timeout

Hi Guys:

I feel stupid. It was so simple!! Please be patient with me I'm trying to learn Perl.

Are there any good books?

Thanks again, Greg
I know how to do it in pascal.
A. Clay Stephenson
Acclaimed Contributor

Re: Command Timeout

There are several good perl texts. I would get all the O'Reilly 'Camel' books. 'Learning Perl', 'Perl Programming' , 'Advancing Perl Programming' and the 'Perl Cookbook' are all excellent. If you need to do any work on W2K or NT, I would also get 'Learning Perl on Win32 Systems'. I've even got NT guys doing Perl now and the nice part is that most of these work unchanged on either UNIX or NT.



Clay
If it ain't broke, I can fix that.