Creating a Service with automator, caffeinate

automatorbashsleep-waketerminal

First of all, sorry for my english, not a native speaker.
Since I'm a bit fed up opening Termninal and ^C I want to create a shortcut for activating caffeinate -d on my Mac OS X 10.11
I've been trying a simple Automator Service but with my noob skills it doesn't work.

This is the settings:
I chose Service as type of document

Library->Utilities->Run Shell Script
Service receives: no input
in: any application
Shell: /bin/bash
Pass input: to stdin

CAFFEINATECHECK=`ps | grep caffeinate | cut -d ' ' -f7,8`
if [ $CAFFEINATECHECK == 'caffeinate -d' ]; then
killall caffeinate
else
caffeinate -d
fi

Problem is that such a short scrip keeps running with no response and I have to stop it after a minute
I hope someone could give me any tips necessary

Best Answer

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 &
else
    pkill caffeinate
fi