# Simple C or C++ to put computer to sleep immediatly?



## profx (Mar 27, 2003)

how do i write a simple C utility that will put the computer to sleep straight away from the unix promt?

I want to use it as part of a script to be run from cron.

Dont say use applecript, cause it don't work when run from the unix promt.

Any help apprecaiated!


----------



## lurk (Mar 27, 2003)

I am afraid that applescript was going to be my suggestion.  Why won't it work from the prompt or more importantly when started by cron?  I haven't done that often but it always worked for me...

-Eric


----------



## profx (Mar 27, 2003)

wite an applescript say:
tell application "finder"
sleep
end tell

this will work fine when run from script editor and even from the command promt as

osascript /path/to/sleep.scpt

(sleep.scpt being the script above)


but when run from cron exactly as above, nada.
It dosent do a thing.

Beats me. Top shows osascript running but no sleep.


argh


----------



## Arden (Mar 27, 2003)

First, stop saying "promt."  It's "prompt."  Thanks... 

Second, why can't you just push command-option-power/eject?  This seems much easier than typing something into a command line...

Or are you using single-user mode or some such, and don't have access to normal OS X operations?  Because I know there's a way to shut down your computer from the command line... hold on, lemme look it up... okay, I can't find it, but if you know much about UNIX you probably know what I'm talking about anyway.  I'm sure you can do something for sleep from within UNIX.


----------



## profx (Mar 27, 2003)

sorry, that is just my bad typing i do know how to spell promt i mean prompt!

Okay, i figured it out, you have to save the script as an application, it wont work as a compiled script.


----------



## Arden (Mar 27, 2003)

Oh... like, you have to plug it in.


----------



## profx (Mar 27, 2003)

hey, evrything else can be run from cron via a compiled script...   You try it.  Give me a logical explanation why an applescipt run from cron is different from running from it from the command prompt which is different from saving the script as an application then running it.

Its bizzare.


----------



## lurk (Mar 27, 2003)

One guess did you use the full path to osascript in your cron job.  It might not be in the path.  There may also be some other issue of environment which you need to set right.  Cron jobs tend to run in very spartin environments.

Just a thought....


----------



## profx (Mar 27, 2003)

yeah the script will run but it ignores the sleep command, it will execute the whole script except for the sleep command and i always use full paths in my scripts /usr/bin/curl etc


----------

