Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

IPC: trouble killing off process

by skillet-thief (Friar)
on Aug 13, 2004 at 15:52 UTC ( [id://382731]=perlquestion: print w/replies, xml ) Need Help??

skillet-thief has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks!

I never thought I would need to bother with IPC, but I guess one should never say things like that.

Here is my current problem: after moving to a place with no possibility of a DSL connection, I am trying to make the most of my dialup connection by having my Linux box connect automatically every night, get my mail with fetchmail and use either wget or LWP to download the sites that I read everyday. The whole thing will be run as a cron job. I am using wvdial to run my dialup.

I need some kind of forking mechanism so that I can connect through wvdial with one process, and do my online stuff with the other, then kill wvdial when I'm done. wvdial waits for a control-c (SIGINT, as far as I know) to exit.

The simplest solution would have been to use sytem("wvdial &"). If I do this, however, I don't have a pid to kill when I'm done, at least not with a perl ipc solution. (I know that I could use ps to get wvdial's pid...)

My attempts with fork and open haven't been much more fruitful, but for different reasons. With fork, when I kill the child process, wvdial survives for some reason. And with

$pid = open WV "-|""wvdial"<code>, <code>$pid
is the pid of the fork, and not that of wvdial itself. So, once again, killing of the fork process isn't enough.

Here is my best try so far, using fork:

#!/usr/local/bin/perl use strict; use warnings; ++$|; print "Starting dial program\n"; my $child; if ( $child = fork ) { print "Parent:\t$$\n"; sleep 20; # wait for wvdial to do login while ( 1 ) { sleep 1; last if netcheck(); # check if network is up } if (( proc_check($child) ) && ( netcheck())) { print "Do stuff\n"; system("fetchmail"); system("wget", "http://www.example.com"); } else { print "Something's wrong: q to get out of here"; } my $in = <>; # this is just a temporary end to the script for te +sting if ( $in eq 'q' ) { kill -2, $child; exit; } } else { ### CHILD print "Child\t$$\n"; system( "wvdial" ); } sub netcheck { my $net = `netstat -r`; if ( $net =~ m!ppp! ) { return 1; } else { return 0; } } # end netcheck sub proc_check { my $pid = shift; unless ( kill 0 => $pid ) { print "Process $pid died or something\n"; return 0; } return 1; } # end proc_check

Thanks in advance for any and all wisdom!

s-t

Replies are listed 'Best First'.
Re: IPC: trouble killing off process
by ikegami (Patriarch) on Aug 13, 2004 at 17:53 UTC
    is the pid of the fork, and not that of wvdial itself.

    That got my attention. Then I noticed you're using system( "wvdial" ); instead of exec( "wvdial" );. Try this with exec().

    system() launches a process and waits for it to finish (so it's appropriate for wget).

    fork() + exec() launches a process that runs in parallel (the appropriate choice for wvdial).

      So advanced, it's simple!

      Thanks a bunch!

      Joe

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://382731]
Approved by VSarkiss
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-24 06:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found