white-fox has asked for the wisdom of the Perl Monks concerning the following question:

hi monks...
how can i terminate a program out of my perl prigram?
for start of program out of my source i use system() but how can i terminate that?

Replies are listed 'Best First'.
Re: how to terminate a program?
by Zaxo (Archbishop) on Mar 30, 2005 at 19:05 UTC

    I'm not sure there is a way outside of a keyboard interrupt (SIGINT) or alarm. "Safe" signal handling of Perl 5.8 may even prevent that. That's because system suspends your program until it returns, so you don't have the child pid, or the opportunity to use it.

    For more control, use fork or open.

    After Compline,
    Zaxo

Re: how to terminate a program?
by brian_d_foy (Abbot) on Mar 30, 2005 at 19:56 UTC

    If you are using system() to start another program, you pretty much have to wait until it's done to do anything else. You could fool around with alarm() to give that part of the code a maximum time that it can process, but you haven't said enough for anyone to suggest a real solution. Post code. :)

    You probably want to fork off (or whatever your operating system does) another process so your main program can continue (and have a chance to interact with other processes).

    --
    brian d foy <brian@stonehenge.com>
Re: how to terminate a program?
by CountZero (Bishop) on Mar 30, 2005 at 19:02 UTC
    Pray, tell us what Operating System you are running?

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: how to terminate a program?
by ikegami (Patriarch) on Mar 30, 2005 at 21:24 UTC
    kill can be used to kill another process.
Re: how to terminate a program?
by Anonymous Monk on Mar 31, 2005 at 12:57 UTC
    Assuming you have the process id of the process you want to terminate in the variable $pid:
    kill 15, $pid and kill 0, $pid and sleep 1 and kill 0, $pid and kill 2, $pid and kill 0, $pid and sleep 1 and kill 0, $pid and kill 1, $pid and kill 0, $pid and sleep 1 and kill 0, $pid and kill 9, $pid and kill 0, $pid and sleep 1 and kill 0, $pid and die "Help, can't kill the process";
    That would try to kill the process sending it SIGTERM, SIGINT, SIGHUP and SIGKILL, in that order, and with a 1 second delay in between. The kill 0 will fail if the process went away, terminating the construct. Note that this will fail to die with the message if it can't kill the process if the process changed UID.

    This might not do the right thing if after sending it a signal, the process doesn't die right away, but dies while the killing program sleeps, and another process, with your UID, is born, and getting the same PID as the process you're trying to kill.

    If you happen to have imported POSIX::sleep, replace sleep with CORE::sleep.

    If the process to be killed takes more than a second to clean itself up, it might be killed by the wrong signal.

    According to perlport, the above will not work on Mac OS nor on RISC OS, and won't give the process to shutdown cleanly on Windows.

Re: how to terminate a program?
by scmason (Monk) on Mar 30, 2005 at 20:28 UTC
    Your best bet is to fork your in own program and let the parent keep track of the childs PID. Make your system() call in the child. When you are ready to terminate it, call kill on the childs PID.

    I would write some code for you, but there is tons out there:

    Link to Perl Cookbook ch 17 section 13 removed Forking
    Link to Perl Cookbook, ch 16 section 01 removed. Process Management
    Link to Perl Cookbook chapter 16 section 03 removed Exactly what you want

    And so on (you might notice that these are all from the Perl Cookbook). Good luck.

    Links to copyrighted material posted without express permission of author and publisher removed to eliminate copyright violation by davido per consideration.