Home: Perl Programming Help: Advanced:
Kill Process in Perl



sha37
Novice

Mar 9, 2010, 12:24 PM


Views: 14216
Kill Process in Perl

Hi Folks,

I have a scheduler shell script. i pass a command to the shell script saying (start and stop)


Code
   

[shap@test]$ sh scheduler.sh start



Inside the shell script


Code
   

#!/bin/sh

if [ -n $1 ];
then
perl AD.pl $1;
fi



The Perl file AD.pl ignores "start" process. But when i pass the shell script "STOP". The perl should kill the process.

I mean the AD.pl should stop running. like forcing it to kill. what is the best way to kill the running process.

in AD.pl the below code i have is


Code
   

if ($ARGV[0] eq 'stop')
{
my $cmd = "ps aux | grep " . $0;
my $result = `$cmd`;
$log->notice("Stopping Follower $followerName");
system(`kill -9 $result`);
exit;
}



This runs well but shows some bunch of errors in the shell script. Is this the best way or can i make it more cleaner?

Thanks in Advance

Sha


(This post was edited by sha37 on Mar 15, 2010, 1:16 PM)


FishMonger
Veteran / Moderator

Mar 9, 2010, 12:52 PM


Views: 14210
Re: [sha37] Kill Process in Perl

What process are you trying to kill?

Logic wise, your code doesn't make any sense.


FishMonger
Veteran / Moderator

Mar 9, 2010, 12:54 PM


Views: 14208
Re: [sha37] Kill Process in Perl


Quote
This runs well but shows some bunch of errors in the shell script.


Well that would tell me that the scripts don't run well, otherwise you would not be getting a bunch of errors.


sha37
Novice

Mar 9, 2010, 1:03 PM


Views: 14205
Re: [FishMonger] Kill Process in Perl

trying to kill AD.pl itself. since $0 gives me the script name AD.pl.

so i want to kill AD.pl file.


(This post was edited by sha37 on Mar 9, 2010, 1:05 PM)


sha37
Novice

Mar 9, 2010, 1:11 PM


Views: 14198
Re: [FishMonger] Kill Process in Perl


Code
  

sh: line 0: kill: shap: arguments must be process or job IDs
sh: line 0: kill: 1.6: arguments must be process or job IDs
sh: line 0: kill: 1.2: arguments must be process or job IDs
sh: line 0: kill: (29356) - No such process
sh: line 0: kill: (13276) - No such process
sh: line 0: kill: pts/5: arguments must be process or job IDs
sh: line 0: kill: S+: arguments must be process or job IDs
sh: line 0: kill: 15:55: arguments must be process or job IDs
sh: line 0: kill: 0:00: arguments must be process or job IDs
sh: line 0: kill: perl: arguments must be process or job IDs
sh: line 0: kill: AD.pl: arguments must be process or job IDs
sh: line 0: kill: start: arguments must be process or job IDs
sh: line 1: shap: command not found
sh: line 2: shap: command not found
sh: line 3: shap: command not found



These are the errors if i run

Code
 sh scheduler.sh stop



FishMonger
Veteran / Moderator

Mar 9, 2010, 1:12 PM


Views: 14197
Re: [sha37] Kill Process in Perl

Do you have other instances of AD.pl running besides this current one and the one you started in the if block?


sha37
Novice

Mar 9, 2010, 1:16 PM


Views: 14195
Re: [FishMonger] Kill Process in Perl

nope i dont have other AD.pl script running other than these two.


FishMonger
Veteran / Moderator

Mar 9, 2010, 1:23 PM


Views: 14194
Re: [sha37] Kill Process in Perl

Then why are you launching 2 instances of a program that you don't want to run in the first place. That clearly doesn't make any sense.


FishMonger
Veteran / Moderator

Mar 9, 2010, 1:29 PM


Views: 14191
Re: [sha37] Kill Process in Perl


Code
exit if $ARGV[0] eq 'stop';



sha37
Novice

Mar 9, 2010, 1:38 PM


Views: 14188
Re: [FishMonger] Kill Process in Perl

I can't exit on stop in the AD.pl.

Because if i run one instance of sh scheduler.sh start in one terminal.

I have to run sh scheduler.sh stop in another command. so i have to find the process id od AD.pl to kill if i run stop.

make sense?


sha37
Novice

Mar 9, 2010, 1:57 PM


Views: 14183
Re: [sha37] Kill Process in Perl

This below thing works without errors.


Code
  

ps ax | grep "$PROCESS_NAME" | awk '{print $1}' | xargs -i kill {} 2&>/dev/null



Thanks for everyone's help.

-Sha


FishMonger
Veteran / Moderator

Mar 9, 2010, 1:58 PM


Views: 14183
Re: [sha37] Kill Process in Perl

That's why I asked if you had other instances running, and you said no you didn't.

See:

man killall
http://linux.die.net/man/1/killall

man killproc
http://man-wiki.net/index.php/8:killproc

You should also take a look at your system's startup and init.d scripts to see the proper procedures for this type of script. Yours appears to be very convoluted.