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


In reply to IPC: trouble killing off process by skillet-thief

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.