CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



sha37
Novice

Mar 9, 2010, 12:24 PM

Post #1 of 12 (5880 views)
Kill Process in Perl Can't Post

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

Post #2 of 12 (5874 views)
Re: [sha37] Kill Process in Perl [In reply to] Can't Post

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

Post #3 of 12 (5872 views)
Re: [sha37] Kill Process in Perl [In reply to] Can't Post


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

Post #4 of 12 (5869 views)
Re: [FishMonger] Kill Process in Perl [In reply to] Can't Post

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

Post #5 of 12 (5862 views)
Re: [FishMonger] Kill Process in Perl [In reply to] Can't Post


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

Post #6 of 12 (5861 views)
Re: [sha37] Kill Process in Perl [In reply to] Can't Post

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

Post #7 of 12 (5859 views)
Re: [FishMonger] Kill Process in Perl [In reply to] Can't Post

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


FishMonger
Veteran / Moderator

Mar 9, 2010, 1:23 PM

Post #8 of 12 (5858 views)
Re: [sha37] Kill Process in Perl [In reply to] Can't Post

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

Post #9 of 12 (5855 views)
Re: [sha37] Kill Process in Perl [In reply to] Can't Post


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



sha37
Novice

Mar 9, 2010, 1:38 PM

Post #10 of 12 (5852 views)
Re: [FishMonger] Kill Process in Perl [In reply to] Can't Post

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

Post #11 of 12 (5847 views)
Re: [sha37] Kill Process in Perl [In reply to] Can't Post

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

Post #12 of 12 (5847 views)
Re: [sha37] Kill Process in Perl [In reply to] Can't Post

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.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives