Creating a Service with automator, caffeinate
If you're trying the check to see if caffeinate is running and terminate it if it is, or start it if it isn't, then I'd use:

#!/bin/bash

if [[ $(pgrep caffeinate) == "" ]]; then
    caffeinate -d &amp;
else
    pkill caffeinate
fi