- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Prevent command from hanging
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
Forums
Discussions
Discussions
Discussions
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
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
06-05-2002 02:19 PM
06-05-2002 02:19 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2002 02:25 PM
06-05-2002 02:25 PM
SolutionA 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2002 02:37 PM
06-05-2002 02:37 PM
Re: Prevent command from hanging
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2002 02:44 PM
06-05-2002 02:44 PM
Re: Prevent command from hanging
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2002 05:10 PM
06-05-2002 05:10 PM
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