0

I try use this command:

start "" putty.exe -ssh -load NameOfSessionInPutty -m "C:\Program Files\PuTTY\MYCOMMAND.txt" -t

MYCOMMAND.txt contains:

sudo su - -c "kill `ps -ef | grep 1.sh`" 

But the above command kills only 1 line (first that it finds), and I need to find and kill ALL processes with this name 1.sh.

When I do this manually:

kill `ps -ef | grep 1.sh`

it works perfectly, killing all processes with this name.

But sudo su - -c "kill `ps -ef | grep 1.sh`" kills only the first one found and closes the session.

Help, please, whoever understands what I need to change in the code.

Kamil Maciorowski
  • 69,815
  • 22
  • 136
  • 202
Kostia
  • 15
  • 5
  • thanks, for quick response, if you kind, help me what command need use?) To have same result from 1line code to use through batch file, as using by hands command "kill 'ps -ef | grep 1.sh'" from terminal – Kostia Oct 05 '21 at 16:33
  • Have you tried putting the script on the "other side" and calling THAT from the batch file? – Señor CMasMas Oct 05 '21 at 17:01
  • When you do `kill …`, the inside of the backticks is evaluated before `kill`. When you do `sudo …`, the inside of the backticks is evaluated before `sudo` (and then [maybe `ps` only sees some PIDs](https://superuser.com/a/704035/432690)). I don't know the mechanics of PuTTY, but if the command is going to be evaluated in Linux then try single-quotes instead of double-quotes. There may be other issues and the resulting command may not be optimal, but at least you will delay the evaluation of the backticks. – Kamil Maciorowski Oct 05 '21 at 17:43
  • Why `sudo su - -c "kill ` ` `ps -ef | grep 1.sh` ` `"` and not just `sudo "kill ` ` `ps -ef | grep 1.sh` ` `"` ? Or probably `sudo 'kill ` ` `ps -ef | grep 1.sh` ` `'` would be even better? – raj Oct 05 '21 at 17:47
  • Thanks for response, @Peregrino69 no problem, @Kamil Maciorowki I try yours advice, when I put single quote in my version sudo su - -c 'kill `ps -ef | grep 1.sh`' I recieve same result as with double quotes: -bash: line 0: kill: root: arguments must be process or job IDs Terminated ...So process terminated, after 1st line, when I try sudo 'kill `ps -ef | grep 1.sh`', I recieve answer: sudo: kill `ps -ef | grep 1.sh`: command not found ... Problem not solved with that... Single quoute makes no difference, other command not work – Kostia Oct 05 '21 at 18:32
  • @Señor CMasMas yes, thanks for advice, it worked for me! I put script on CentOs: `#!/bin/bash sudo su - -c 'kill `ps -ef | grep go2.sh`'` and call it from batch through putty and it remove all process in 1 time, looks like sudo su - -c "...." has some restrictions when is called from "this side" to kill only 1 process and finish. But still interesting is there any solution to make it working from "this side"? – Kostia Oct 05 '21 at 18:54
  • Nope ;) Or I would have posted an answer and not a work-around. :) Good luck figuring it out. – Señor CMasMas Oct 05 '21 at 19:01
  • @Señor CMasMas then, thanks again for good advice) save me many time. I will stop on it – Kostia Oct 05 '21 at 19:03
  • [`pkill`](http://manpages.org/pkill) is much easier to get right – dave_thompson_085 Oct 06 '21 at 01:24
  • dave_thompson_085 thanks man a lot! I just use `pkill -f 1.sh` and it worked for me much better! (Instead of variant above, which I test again from "that side" and not work again, dont know yesterday not test porperly) ..... somehow `sudo su - -c 'pkill -f 1.sh' ` works from "this side" directly from batch file through putty without problem, killing all process with given name! Thanks again, to best answer – @dave_thompson_085 And thanks to all who try help) ! – Kostia Oct 06 '21 at 09:19

3 Answers3

1

Not sure why you combine sudowith su. Seems to me like an overkill to get root-rights. Instead you could use ...

sudo kill ...

or

su kill ... -

To kill all processes named 1.sh you can combine pgrep (to find all the PIDs) with kill (to send a SIGTERM to all those PIDs).

pgrep "1.sh" | xargs kill

You could also use pkill which is the same as explained before but combined all into one command.

pkill "1.sh"

An alternative is killall to send SIGTERM to all processes with that name.

killall "1.sh"
Mario
  • 173
  • 1
  • 5
0

Kill command needs only PIDs(integers), I'm not sure how can you get that with just ps -ef | grep 1.sh

I'm assuming that you've to kill all the process instances of 1.sh script.

If pidof binary is available in your machine, you can just use pidof 1.sh | xargs sudo kill, put this command inside your MYCOMMAND.txt file

Vaibhav S
  • 31
  • 4
  • when I put that command it give me answer: `pidof 1.sh | xargs sudo kill` Usage: kill [options] [...] Options: ......... For more details see kill(1). ...... Maybe I will try this `ps -ef | grep 1.sh | xargs sudo kill` .....when I try last it give answer: ..... kill: cannot find process "root" Terminated ......, so only working answer for me now (from @Señor CMasMas), is to start from "that side" from script .sh – Kostia Oct 06 '21 at 04:48
0

Best answer: use other command sudo su - -c 'pkill -f 1.sh' in txt file, instead of sudo su - -c "kill ps -ef | grep 1.sh" .

Works properly.

Finding and killing all process with given name "1.sh", as example.

Thanks, @dave_thompson_085 for giving advice, to try pkill (that easier to get right for it)

Kostia
  • 15
  • 5