Kill a system process within the script

Discussion in 'Perl Misc' started by Mav, Jun 3, 2004.

  1. Mav

    Mav Guest

    Hi, all
    I am trying to lanuch a command on my perl script using system call,
    I wonder is that a way when someone hit Ctrl-Y, it will kill my
    script,and also kill that system call process as well.

    My script:

    ....
    @args = ("doing something take a long time");

    if (system(@args)==0) {
    print "ok";
    } else
    print "something wrong";

    Please help,
    Thanks,
    M
     
    Mav, Jun 3, 2004
    #1
    1. Advertising

  2. Mav wrote:

    > Hi, all
    > I am trying to lanuch a command on my perl script using system call,
    > I wonder is that a way when someone hit Ctrl-Y, it will kill my
    > script,and also kill that system call process as well.


    Huh? killing a process takes microseconds.

    > My script:
    >
    > ...
    > @args = ("doing something take a long time");
    >
    > if (system(@args)==0) {
    > print "ok";
    > } else
    > print "something wrong";
    >
    > Please help,
    > Thanks,
    > M


    Assuming linux/unix:
    Try system("kill -SIGTERM $process") ; then
    Try system("kill -SIGKILL $process")

    where you set $process top the pid you want tokill.


    gtoomey
     
    Gregory Toomey, Jun 3, 2004
    #2
    1. Advertising

  3. Mav

    Anno Siegel Guest

    Mav <> wrote in comp.lang.perl.misc:
    > Hi, all
    > I am trying to lanuch a command on my perl script using system call,
    > I wonder is that a way when someone hit Ctrl-Y, it will kill my
    > script,and also kill that system call process as well.
    >
    > My script:
    >
    > ...
    > @args = ("doing something take a long time");
    >
    > if (system(@args)==0) {
    > print "ok";
    > } else
    > print "something wrong";


    I don't know what Ctrl-Y does in your system. An INT signal (as
    created by Ctrl-C per default on Unix) is already treated the way
    you want.

    Anno
     
    Anno Siegel, Jun 4, 2004
    #3
  4. Mav

    lee Guest

    Mav wrote:

    > I am trying to lanuch a command on my perl script using system call,
    > I wonder is that a way when someone hit Ctrl-Y, it will kill my
    > script,and also kill that system call process as well.


    Yes.

    When you launch the process, note its process ID.

    Then have your script "trap" the control character in question,
    and when that character is received, call the system 'kill'
    command
    on the previously noted PID, as described by someone else on
    this thread.

    See, for example, perldoc -q signal:

    Found in .... pod/perlfaq8 :
    How do I trap control characters/signals?

    $Interrupted = 0; # to ensure it has a value
    $SIG{INT} = sub {
    $Interrupted++;
    syswrite(STDERR, "ouch\n", 5);
    }


    Lee Goddard
     
    lee, Jun 5, 2004
    #4
  5. Mav

    Mav Guest

    The script is running on the PC.
    Once I launch the "system("Doing something take a long time");" from
    my perl script(doit.pl), I hope when the user kill my doit.pl, it will
    kill the
    "system("Doing something take a long time");" process as well, I
    really don't want leave the process behind.

    Is that anywhere I can find code example doing such thing?

    Thanks a lot,
    Mav




    lee <> wrote in message news:<>...
    > Mav wrote:
    >
    > > I am trying to lanuch a command on my perl script using system call,
    > > I wonder is that a way when someone hit Ctrl-Y, it will kill my
    > > script,and also kill that system call process as well.

    >
    > Yes.
    >
    > When you launch the process, note its process ID.
    >
    > Then have your script "trap" the control character in question,
    > and when that character is received, call the system 'kill'
    > command
    > on the previously noted PID, as described by someone else on
    > this thread.
    >
    > See, for example, perldoc -q signal:
    >
    > Found in .... pod/perlfaq8 :
    > How do I trap control characters/signals?
    >
    > $Interrupted = 0; # to ensure it has a value
    > $SIG{INT} = sub {
    > $Interrupted++;
    > syswrite(STDERR, "ouch\n", 5);
    > }
    >
    >
    > Lee Goddard
     
    Mav, Jun 15, 2004
    #5
  6. Mav

    Mothra Guest

    Mav wrote:

    > The script is running on the PC.
    > Once I launch the "system("Doing something take a long time");" from
    > my perl script(doit.pl), I hope when the user kill my doit.pl, it will
    > kill the
    > "system("Doing something take a long time");" process as well, I
    > really don't want leave the process behind.
    >
    > Is that anywhere I can find code example doing such thing?
    >

    Try Chapter 16 of "Perl Cookbook" published by O'Reilly - there's loads
    of examples in there.
     
    Mothra, Jun 15, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carlo Filippini

    Kill win32 process

    Carlo Filippini, Jan 7, 2004, in forum: Perl
    Replies:
    0
    Views:
    1,113
    Carlo Filippini
    Jan 7, 2004
  2. John A. Bailo

    Kill Thread Within Java.exe Process

    John A. Bailo, Aug 3, 2007, in forum: Java
    Replies:
    3
    Views:
    1,980
    Mike Schilling
    Aug 3, 2007
  3. Replies:
    1
    Views:
    311
    Douglas Wells
    Apr 19, 2008
  4. Howard
    Replies:
    4
    Views:
    105
    Thomas Kratz
    Oct 27, 2004
  5. priya
    Replies:
    2
    Views:
    130
Loading...

Share This Page