I found a great Tip from Brett Terpstra's website to help you keep your Mac from going to sleep. In the past I have used a app called Caffeine. It is a tiny program that puts an icon in the right side of your menu bar. You can click it to prevent your Mac from automatically going to sleep, dimming the screen or starting screen savers. Click it again to go back.
Brett pointed out there’s a command called caffeinate that does exactly what the app Caffeine does. This command can be used both with scripts and in the command line. It also allows you to add a timer and tell it to prevent sleep for a certain duration. Alternatively, you can attach it to another command to keep the computer awake as long as that process is running, allowing the system to sleep after the command finishes.
Here are some of the available options from the Mac Developer Library.
-d Create an assertion to prevent the display from sleeping.
-i Create an assertion to prevent the system from idle sleeping.
-m Create an assertion to prevent the disk from idle sleeping.
-s Create an assertion to prevent the system from sleeping. This assertion is valid only when sys-tem system tem is running on AC power.
-u Create an assertion to declare that user is active. If the display is off, this option turns the display on and prevents the display from going into idle sleep. If a timeout is not speci-fied specified fied with '-t' option, then this assertion is taken with a default of 5 second timeout.
-t Specifies the timeout value in seconds for which this assertion has to be valid. The assertion is dropped after the specified timeout. Timeout value is not used when an utility is invoked with this command.