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
caffeinate -d

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

1 Answer 1


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:


if [[ $(pgrep caffeinate) == "" ]]; then
    caffeinate -d &
    pkill caffeinate
  • That surely is more elegant than my first draft. At first your code haven't worked, it kept running like mine for longer than it should, but I remembered reading somewhere that Automator doesn't work well with '&' AAAND INDEED it works (workflow completed in 0,218 seconds) by removing that out of the equation. Thanks alot, much appreciated
    – tebkanlo
    Mar 2, 2020 at 7:56
  • I experienced it before that a working bashscript wasn't running inside Automator environment. I still wonder what was that didn't let my code works smoothly afterall.
    – tebkanlo
    Mar 2, 2020 at 8:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .