change:
grep "^${pid}$" 2>&1

to:
grep -qs "^${pid}$"

other than that its a very nice fix!