Kill a Process via a shell script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sha37
    New Member
    • Feb 2010
    • 4

    Kill a Process via a shell script

    I have a shell script scheduler.sh passing command line arguments start and stop. Below is the code.

    Code:
    #!/bin/sh
    if [ -n $1 ];
    then
            perl AD.pl $1;
    fi
    [shap@test]$ sh scheduler.sh stop
    If i pass stop the AD.pl file should stop running by killing its pid.
    Below is the code for AD.pl when it sees Stop from shell.
    Code:
    if ($ARGV[0] eq 'stop')
    {
        my $cmd = "ps aux | grep " . $0;
        my $result = `$cmd`;
        $log->notice("Stopping Follower $followerName");
        system(`kill -9 $result`);
        $signal = 0;
    }
    Please suggest a cleaner way to above code. is the way i am doing is correct?
    Is there a cleaner way of doing it.

    Thanks in advance
    Sha
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    I'm pretty sure the only way to do it is to use the kill command.

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      You can either issue a shell command in back tics, as RedSon suggested, or, you can use the built in kill command in Perl.

      I have not tested it myself, but would certainly test it on something non-production before going live with it.

      Regards,

      Jeff

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        Cross posted http://perlguru.com/gforum.cgi?post=...=unread#unread

        Comment

        Working...