0
$ps -fu $USER
UID        PID  PPID  C STIME TTY          TIME CMD
kanoujip 22471 21858  0 Sep16 pts/3    00:00:00 /bin/sh ./Alert_notification.sh
kanoujip 27450 27390  0 Sep16 ?        00:00:00 sshd: kanoujip@pts/9

I want to run another time ./Alert_notification.sh again so want to kill all previous runs from my User ID

How can I achieve this ?

ps -fu $USER | grep Alert_notification.sh |grep -vE 'grep|"$PPID"' | awk '{print $2}' | xargs kill `ps -o pid= -N T` ;

terminates all the MY USER session

  • Just change your second grep to grep -v grep. – ott-- Sep 17 '16 at 20:22
  • I tried that . I think i didn't explained well. Suppose 1st Alert_notification.sh script is running in one terminal (or in back ground) now i want to invoke another Alert_notification.sh script which should close all previous instance of Alert_notification.sh running from my User ID on any terminal. killing Process is part of Alert_notification.sh script. – kanoujiapraveen Sep 17 '16 at 20:32
  • Ok, can you change the expression to just grep|$PPID? The the' suppresses parameter expansion. – ott-- Sep 17 '16 at 21:32
0

If I understand well your request, you don't have to grep on the $PPID variable, that is used, maybe, even by your last called script execution, but on the PID of the last called script: $$.

If you call script.sh using the same bash, $PPID willi be the same for all the others execution of the script script.sh (or Alert_notifications.sh in your case).

Try changing something into your command and adding a little of control (if you want):

ps -fu $USER | grep  "Alert_notification.sh" | grep -v "grep\|$$" | awk '{print $2}' | xargs kill 2> /dev/null  
  • 2> /dev/null: you need it to omit error messages in the case some it will be found no process

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.