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.

SCL
  • 855
  • 1
  • 10
  • 13

2 Answers2

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
Jay
  • 6,439
  • 24
  • 34
  • 3
    Why not using pkill directly? :) (And - why -9? 'kill -kill' is so harmful...) – Michuelnik Jun 14 '12 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 `SIGTERM`s so there might still be a genuine use to script it. – Jay Jun 14 '12 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 '12 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 '12 at 16:34
  • But this worked: kill -9 `pgrep -f "MailSender.exe"`. Thanks! – SCL Jun 14 '12 at 16:34
  • 1
    @Dawkins You should be able to do `pkill -f "Mailsender.exe"` – cjc Jun 14 '12 at 17:33
  • WARNING about using pkill: the -v parameter does something else than what one would normally expect a -v flag to do, and does so very tersely indeed..... "something else" is extremely destructive. – rackandboneman Jun 15 '12 at 00:16
  • `-v` is consistent with all the grep family: it negates the resultset. Who is using `-v` here? I don't follow. – Jay Jun 15 '12 at 00:48
-1

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

Warner
  • 19