Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

start and kill a process in a local shell

by danmcb (Monk)
on Oct 12, 2007 at 09:35 UTC ( [id://644413]=perlquestion: print w/replies, xml ) Need Help??

danmcb has asked for the wisdom of the Perl Monks concerning the following question:

I want to run a program on a local machine that does not exit itself. I want to capture it's output, and then send it a CTRL-C to stop it. What's the easiest way?

  • Comment on start and kill a process in a local shell

Replies are listed 'Best First'.
Re: start and kill a process in a local shell
by shmem (Chancellor) on Oct 12, 2007 at 10:35 UTC
    See open. Opening a pipe to a program returns that programs PID.
    $pid = open $pipe, "/some/program |"; while (<$pipe>) { ... # /some/program's output in $_ } close $pipe; # program gets a sigpipe if (kill 0, $pid) { # still there? kill 15, $pid; }

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: start and kill a process in a local shell
by oha (Friar) on Oct 12, 2007 at 10:28 UTC
    check IPC::Open2, it will open a pipe to a command, and return the PID, with the pid you can send a kill

    Oha

Re: start and kill a process in a local shell
by apl (Monsignor) on Oct 12, 2007 at 09:56 UTC
    I'm assuming this is a daemon running in the background on a *nix box.

    To capture it's output, have crontab or whatever starts it redirect to some standard directory with a filename containing the date and time the process started.

    As far as stopping it is concerned, don't send a CTRL-C, use a kill -9 <id> where <id> is the process-number of the program.

      Signal KILL (9) is last resort and should be used as such. A process killed that way has no chance to clean up resources (e.g. flush buffers, close filehandles).

      Signals 1, 2, 3 or 15 should be used, an 9 only when the process is still running after those.

        You are correct. But when I saw a program (...) that does not exit itself, I made the unwarranted assumption that the original writer of the program didn't seem particularly interested in cleaning up.
Re: start and kill a process in a local shell
by cdarke (Prior) on Oct 12, 2007 at 12:09 UTC
    > then send it a CTRL-C to stop it.

    CTRL-C is a SIGINT, so it is kill INT,pid;
Re: start and kill a process in a local shell
by cmv (Chaplain) on Oct 12, 2007 at 13:14 UTC
    I recently had to do something similar, and stumbled into a rats nest of problems revolving around SIGSTOP, and process groups.

    In my scenario, I had a perl script starting up and managing a unix process on a Sun workstation. Anytime I hit ctl-z at my shell prompt (the shell that started the perl script), both the perl script and the background unix process stopped. This is not what I wanted at all.

    After much gnashing of teeth and tearing out hair (I should have asked here), I finally figured out that the SIGSTOP was being sent to everyone in my process group. To solve the problem, I needed to create the unix process in its own process group. This is how I learned about the wonders of the perl "setpgrp" command, and process groups in general.

    So here is my code:

    # Fork a unix process... my $pid; if($pid = fork) { print STDERR "parent pid=$$ child pid=$pid\n"; }elsif (defined $pid) { print STDERR "child pid=$$ pid=$pid\n"; # Must do setpgrp as child for nohup and job control stuff... setpgrp(0, $$); exec $unixcmd || die "Bad exec $!"; } kill 'INT', $pid;
    Hope that helps.

    -Craig

    UPDATE:

    oha opened my mind, by letting me know that the setpgrp could be done from either the parent or the child process. oha++ Thanks!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-25 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found