How to caffeinate a function on CLI?
caffeinate launches executables. However, shell functions exist only within the shell. Shell functions are not executables.

Workaround: call bash directly

This works best for one-liners and simple commands. 

caffeinate bash -c 'sleep 2'


Multi-liners work too:

caffeinate bash &lt;&lt;EOF
  sleep 2
  echo "done :D"
EOF


You can use functions:

caffeinate bash &lt;&lt;EOF
  function mysleep {
    sleep 2
  }
  mysleep
EOF


Workaround: call a script instead of a function

Instead of doing your work inside a function, split it out into a separate script:

caffeinate mysleep.sh




#!/bin/bash
# mysleep.sh
sleep 2