LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-10-2008, 04:34 PM   #1
WindowBreaker
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 228

Rep: Reputation: 31
how to autokill a hanging subprocess from a script


I have a perl script which launches a system command repeatedly from a looping control structure. Occasionally, one instance of that command will hang indefinitely until I manually kill it with 'pkill cmdname' from a shell. Then the loop will continue where it left off.

How can I execute a system command in perl, but autokill that command if it takes longer than 60 seconds to complete?
 
Old 07-10-2008, 10:22 PM   #2
theNbomr
Senior Member
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 4,506

Rep: Reputation: 602Reputation: 602Reputation: 602Reputation: 602Reputation: 602Reputation: 602
Something like this should get you started.
Code:
#! /usr/bin/perl -w
use strict;
use POSIX ":sys_wait_h";

    my $kidPid = fork();
    if( $kidPid ){
        print "Parent has child $kidPid\n";
        sleep( 10 );
        my $status = waitpid( -1, WNOHANG );
        print "Wait status: $status\n";
        if( 0 == $status ){
            print "Die, child ",$kidPid,"\n";
            kill( 9, $kidPid );
        }
    }
    else{
    
        # I'm the child
        my $i = 0;
        while( 1 ){
            print $i++,"\n";
            sleep 1;
        }

    }
    exit( 0 );
If the child process is an external program, you can launch it with exec().

--- rod.
 
Old 07-10-2008, 10:25 PM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 57
I'd be more inclined to discover why the script is hanging in the first place...
 
Old 07-11-2008, 12:25 AM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.2, Centos 5.8
Posts: 11,740

Rep: Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905
Mr C has a good point.
Going by the Perl principle of TMTOWTDI, you could also use IPC::Open2 (http://perldoc.perl.org/IPC/Open2.html) or even IPC::Open3 ( http://perldoc.perl.org/IPC/Open3.html) and wrap a SIGALRM ( http://perldoc.perl.org/functions/alarm.html) around it for closer control/checking.
 
Old 07-11-2008, 02:07 AM   #5
WindowBreaker
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 228

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by Mr. C. View Post
I'd be more inclined to discover why the script is hanging in the first place...
Good question. It's a compiled program called khtml2png2 used to gather screenshots. About 1 out of 20 times it will just hang for hours on end. I've compiled and ran the thing on multiple computers with different linux distro's - same problem.

Thanks to everyone for the responses. I ended up using perl's 'alarm' function. In the hopes that it will help somebody else, here's how I did it:

Code:
$SIG{ALRM} = \&timed_out;

eval{
 alarm(60);
 system('command';
 alarm(0);
};

sub timed_out{ die; }
 
Old 07-11-2008, 02:16 AM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 57
Ok, I see, it is someone else's poorly-written piece of software that hangs. Apparently, these issues have been around for a while with this software. You might want to report it to the author though.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dpkg -i goes wrong because of a piping error to/from subprocess wilsonsamm Linux - General 1 06-09-2008 10:51 PM
Error: subprocess dpkg-split killed by signal (Segmentation fault) Jinouchi Linux - Software 0 06-06-2008 07:44 PM
subprocess failed during install ChrisR(SQL DBA) Suse/Novell 1 10-24-2004 11:47 AM
AWK script is hanging our server Hans Zarkoff Programming 10 08-20-2004 01:32 PM
subprocess dpkg-split killed by signal (Segmentation fault) cparker15 Debian 5 07-28-2004 11:45 AM


All times are GMT -5. The time now is 03:21 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration