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.


In reply to Re: how to terminate a program? by Anonymous Monk
in thread how to terminate a program? by white-fox

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.