3

after the kill statement the script prints "Terminated" and the following lines never get executed:

#!/bin/bash

kill -9 `ps -ef | grep MailSender | grep -v grep | awk '{print $2}'`
echo starting
./MailSender

I have even tried to add set +e at the beginning but it still exits after kill.

2 Answers 2

3

Does the name of your script include MailSender? Try changing it, if so. Also, use pkill -9, it'll be cleaner:

#!/bin/bash

pkill -9 MailSender
echo starting
./MailSender
9
  • 3
    Why not using pkill directly? :) (And - why -9? 'kill -kill' is so harmful...)
    – Michuelnik
    Jun 14, 2012 at 16:28
  • Guess I didn't optimise the code far enough, thanks :-) I'm hoping this is a last-resort script, so I didn't argue with the use of SIGKILL. Admittedly, I have used to kill processes when they would lockup but still absort SIGTERMs so there might still be a genuine use to script it.
    – Jay
    Jun 14, 2012 at 16:31
  • pkill doesnt work, I guess its because MailSender is an argument: /opt/mono-2.10/bin/mono /etc/sender/MailSender.exe
    – SCL
    Jun 14, 2012 at 16:33
  • And if you are under Solaris, don't use killall, which would work the same as pkill under Linux but literally kill all processes under Solaris.
    – Sven
    Jun 14, 2012 at 16:34
  • 1
    @Dawkins You should be able to do pkill -f "Mailsender.exe"
    – cjc
    Jun 14, 2012 at 17:33
-1

pkill will kill anything contained MailSender, maybe your script contain MailSender in its name. Change it.

1
  • 3
    No improvement over accepted answer. Jan 15, 2015 at 5:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .