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.

Kostia
  • 15

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
  • 203
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

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