$! is the PID of the last backgrounded process. kill -0 $PID checks whether it's still running. $$ is the PID of the current shell.