An application I use downloads data from a specific server, and I need it to sleep after the download has finished and the socket is closed.

However downloads take place at random days, the app that downloads checks for updates and only downloads if there is one

Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)    
tcp4       0      0      ESTABLISHED

Pretend is server where data is downloaded from.

Is there a way to prevent the computer sleeping while there is HTTP session to a specific server in progress?

Now please excuse my pseduo script, but it's the best way I could try

netstat -anp tcp 
IF Foreign Address = & state = ESTABLISHED
    pmset sleepnow

if you convert your psudo code to sh, here is a test for your if

netstat -anp|grep EST|sed 's/^..p6\? \+[^ ]\+ \+[^ ]\+ \+[^ ]\+ \+\([^ ]\+\) \+.*$/\1/'|grep -q && echo connected

so you need a while loop and a sleep 10m and a pmset 0 but as my osx box is in storage it would be a good idea to check my re.

  • Thank you so much, this has helped immensely and now I have got something that works. – The Regulator Nov 29 '13 at 11:40

Since Mac OS X 10.8, you should be able to use the caffeinate(8) command.

Try caffeinate -s curl http://example.com/some/url.

  • Ahh, excellent thanks - thought this was mavericks only for some reason. – The Regulator Nov 29 '13 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.