By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,179 Members | 970 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,179 IT Pros & Developers. It's quick & easy.

Kill a Process via a shell script

P: 4
I have a shell script passing command line arguments start and stop. Below is the code.

Expand|Select|Wrap|Line Numbers
  1. #!/bin/sh
  2. if [ -n $1 ];
  3. then
  4.         perl $1;
  5. fi
[shap@test]$ sh stop
If i pass stop the file should stop running by killing its pid.
Below is the code for when it sees Stop from shell.
Expand|Select|Wrap|Line Numbers
  1. if ($ARGV[0] eq 'stop')
  2. {
  3.     my $cmd = "ps aux | grep " . $0;
  4.     my $result = `$cmd`;
  5.     $log->notice("Stopping Follower $followerName");
  6.     system(`kill -9 $result`);
  7.     $signal = 0;
  8. }
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
Mar 9 '10 #1
Share this Question
Share on Google+
3 Replies

Expert 5K+
P: 5,000
I'm pretty sure the only way to do it is to use the kill command.
Mar 9 '10 #2

Expert Mod 2.5K+
P: 3,503
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.


Mar 10 '10 #3

Expert Mod 100+
P: 589

Post your reply

Sign in to post your reply or Sign up for a free account.