- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Command Timeout
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-03-2001 04:15 PM
тАО12-03-2001 04:15 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-03-2001 04:32 PM
тАО12-03-2001 04:32 PM
Re: Command Timeout
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-03-2001 04:36 PM
тАО12-03-2001 04:36 PM
SolutionWhile 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-03-2001 04:57 PM
тАО12-03-2001 04:57 PM
Re: Command Timeout
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-03-2001 05:07 PM
тАО12-03-2001 05:07 PM
Re: Command Timeout
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-03-2001 05:12 PM
тАО12-03-2001 05:12 PM
Re: Command Timeout
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-03-2001 05:21 PM
тАО12-03-2001 05:21 PM
Re: Command Timeout
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-04-2001 08:23 AM
тАО12-04-2001 08:23 AM
Re: Command Timeout
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-04-2001 08:36 AM
тАО12-04-2001 08:36 AM
Re: Command Timeout
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-04-2001 08:38 AM
тАО12-04-2001 08:38 AM
Re: Command Timeout
2>&1
This tells the shell to sened stderr to the same file descriptor as stdout
HTH
Duncan
I am an HPE Employee

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-04-2001 09:06 AM
тАО12-04-2001 09:06 AM
Re: Command Timeout
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-04-2001 09:14 AM
тАО12-04-2001 09:14 AM
Re: Command Timeout
Clay