How to execute kill `ps -ef | grep 1.sh` command as SUDO SU from batch file through Putty terminal to kill ALL process, with given name not only 1st?

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.

10

3 Answers

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

1

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)

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"

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like