I need a function for a script that gets pid for clipit (clipboard manager) and kills clipit. Pretty straightforward stuff I think except for some reason the shell is puking back an error about killing the PID. Here's my function:
stat=0
while [[ "$stat" == 0 ]] ; do
clipitPid=$(ps aux|egrep -m 1 "[ \t]{1,}[0-9]{1,2}:[0-9]{1,2}[ \t]*clipit$"|sed -r "s/^[^ ]*[ ]*([0-9]{3,5})[ \t]{1,}.*$/\1/") #; echo "\$clipitPid: ${clipitPid}"
kill "$clipitPid" 0>/dev/null 1>/dev/null 2>/dev/null
stat="$?"
done
and here is the terminal error:
10:12 ~ $ stat=0 ; while [[ "$stat" == 0 ]] ; do clipitPid=$(ps aux|egrep "[ \t]{1,}[0-9]{1,2}:[0-9]{1,2}[ \t]*clipit$"|sed -r "s/^[^ ]*[ ]*([0-9]{3,5})[ \t]{1,}.*$/\1/") ; echo "\$clipitPid: ${clipitPid}" ; kill "$clipitPid" 0>/dev/null 1>/dev/null 2>/dev/null ; stat="$?" ; done
$clipitPid: 24760
24791
bash: kill: 24760
24791: arguments must be process or job IDs
I've tried removing the sending output to null and that makes no difference. I have two instances of clipit running to ensure both PIDs are detected and killed; however, the PIDs are echoed ok but not being nuked. But if I manually 'kill 24760 24791' the PIDs are killed:
10:12 ~ $ kill 24760 24791
10:12 ~ $ ps aux|grep clipit
[3]- Terminated clipit
[4]+ Terminated clipit
Any ideas what I have wrong here I would appreciate reading them.
EDIT
actually if I have only one instance of clipit running then it gets killed using the above onliner fine. 2 clipits or more then it fails to kill any, yet retrieves the PIDs ok.