Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 12, 2012, 01:51 PM   #1
dasx
macrumors 65816
 
dasx's Avatar
 
Join Date: Jun 2012
Location: Barcelona
SOLVED IT! [Bash script, caffeinate - help]

Hey there.
I sometimes leave my computer at night uploading files to an FTP server. It sometimes needs to upload just 300MB, sometimes 3GB.

In Lion, I had no problems at all. I started uploading, turned screen off (Shift+Control+Eject that is) and went to sleep. The Mini would keep uploading till done, and then would go to sleep too.

In Mountain Lion… well, can't really do that anymore. If I start uploading something and then turn the screen off, the Mini will go to sleep in a 1 minute to 5 minutes bracket, no matter what. (Sleep is set to 12 minutes, so weird… And I don't wanna disable it as I like it to go sleep when not in use).

I then read about caffeinate, and tried estimating how long it'd take to upload all the files and ran:

Code:
$ caffeinate -t 7200
That keeps my computer awake for 2 hours and then puts it to sleep. So pretty nice.

I then decided to create a program which would monitor the upload speed and put the Mini to sleep as soon as it's done.

Problem is it isn't working. I mean, the program works but it's like if no caffeinate process was executed. Can't figure out why.

Any help?

Here's the code:


Code:
coffee(){
        count=0
        while [ $count -lt 5 ]
                do
                        a=`netstat -ibI en1 | awk '$3 ~ /Link/ {print $10}'`
                        sleep 5
                        b=`netstat -ibI en1 | awk '$3 ~ /Link/ {print $10}'`
                        in=$(((b-a)/1024/5))
                        if [ $in -gt 50 ]
                                then    echo "Downloading at $in KB/s"
                                        echo "Waiting 5 minutes..."
                                        echo ""
                                        count=0
                                        caffeinate -t 300 
                                else
                                        count=$((count+1))
                                        echo "Wait $count of 5... (1 minute)"
                                        echo ""
                                        caffeinate -t 60
                        fi
                done
        echo ""
        echo "LET'S SLEEP"
        echo ""
}
Idea: See average upload speed in 5 seconds. If higher than 50KB/s, caffeinate the computer for 5 minutes. Check again. As soon as it's lower, it'll start a 5 minute wait devided in 5 to check if the speed drop was puntual.
If upload speed is lower than 50KB/s for 5 minutes, then program quits and computer is free to go to sleep as soon as it wants to.

As I said though, it isn't working.
__________________
Grey computer which does complicated stuff.
Cell phone that can talk to me. (Seriously).
"Fancy quote from dead guy"

Last edited by dasx; Nov 21, 2012 at 04:16 PM.
dasx is offline   0 Reply With Quote
Old Nov 12, 2012, 05:42 PM   #2
chown33
macrumors 603
 
Join Date: Aug 2009
See here:
http://osxdaily.com/2012/08/03/disab...inate-command/

Read the last comment, dated September 14, 2012 at 9:51 am.
I'm not running Mtn Lion on anything right now, so I can't confirm this myself.


Found by googling: mac os x caffeinate
chown33 is offline   0 Reply With Quote
Old Nov 13, 2012, 05:51 PM   #3
dasx
Thread Starter
macrumors 65816
 
dasx's Avatar
 
Join Date: Jun 2012
Location: Barcelona
Quote:
Originally Posted by chown33 View Post
See here:
http://osxdaily.com/2012/08/03/disab...inate-command/

Read the last comment, dated September 14, 2012 at 9:51 am.
I'm not running Mtn Lion on anything right now, so I can't confirm this myself.


Found by googling: mac os x caffeinate
Thanks for the reply.
That would make sense but when I run the command itself (caffeinate -t xxx) it seems to work like a charm. Always.

That's why I can't get around with this.
__________________
Grey computer which does complicated stuff.
Cell phone that can talk to me. (Seriously).
"Fancy quote from dead guy"
dasx is offline   0 Reply With Quote
Old Nov 13, 2012, 06:18 PM   #4
pitaya
macrumors member
 
Join Date: Jun 2012
The caffeinate man page shows:
Code:
caffeinate [-disu] [-t timeout] [utility] [argument ...]
...
     If a utility is specified, caffeinate creates the assertions
     on the utility's behalf, and those assertions will persist for the dura-
     tion of the utility's execution. 
...
If you can use a command line ftp client (ncftpput, for example. ncftp is in macports), you could try to run it via caffeinate.
pitaya is offline   0 Reply With Quote
Old Nov 15, 2012, 05:26 AM   #5
dasx
Thread Starter
macrumors 65816
 
dasx's Avatar
 
Join Date: Jun 2012
Location: Barcelona
Quote:
Originally Posted by pitaya View Post
The caffeinate man page shows:
Code:
caffeinate [-disu] [-t timeout] [utility] [argument ...]
...
     If a utility is specified, caffeinate creates the assertions
     on the utility's behalf, and those assertions will persist for the dura-
     tion of the utility's execution. 
...
If you can use a command line ftp client (ncftpput, for example. ncftp is in macports), you could try to run it via caffeinate.
Tried that, and worked, but any ideas why the program isn't working? It's driving me crazy! Need to know!
__________________
Grey computer which does complicated stuff.
Cell phone that can talk to me. (Seriously).
"Fancy quote from dead guy"
dasx is offline   0 Reply With Quote
Old Nov 15, 2012, 02:44 PM   #6
cqexbesd
macrumors member
 
Join Date: Jun 2009
Quote:
Originally Posted by dasx View Post
Tried that, and worked, but any ideas why the program isn't working? It's driving me crazy! Need to know!
Use sh -x (or bash -x if you are using bash) to give you some debugging output.
cqexbesd is offline   0 Reply With Quote
Old Nov 16, 2012, 11:55 AM   #7
dasx
Thread Starter
macrumors 65816
 
dasx's Avatar
 
Join Date: Jun 2012
Location: Barcelona
Quote:
Originally Posted by cqexbesd View Post
Use sh -x (or bash -x if you are using bash) to give you some debugging output.
Can't run the app when previously ran $bash -x.
__________________
Grey computer which does complicated stuff.
Cell phone that can talk to me. (Seriously).
"Fancy quote from dead guy"
dasx is offline   0 Reply With Quote
Old Nov 16, 2012, 04:10 PM   #8
cqexbesd
macrumors member
 
Join Date: Jun 2009
Quote:
Originally Posted by dasx View Post
Can't run the app when previously ran $bash -x.
And no one can help you if you aren't a bit more precise :-P

What did you type? What was the output?

I'm assuming the $ in your comment is just a typo?
cqexbesd is offline   0 Reply With Quote
Old Nov 21, 2012, 04:16 PM   #9
dasx
Thread Starter
macrumors 65816
 
dasx's Avatar
 
Join Date: Jun 2012
Location: Barcelona
Found the problem!
Looks like when system is ready to sleep, it doesn't as caffeinate is running. But as soon as it ends and the script should check again if run caffeinate again or not, the system puts itself to sleep.

So I modified the code. In red.

Code:
coffee(){
        caffeinate &
        count=0
        while [ $count -lt 5 ]
                do
                        a=`netstat -ibI en1 | awk '$3 ~ /Link/ {print $10}'`
                        sleep 5
                        b=`netstat -ibI en1 | awk '$3 ~ /Link/ {print $10}'`
                        in=$(((b-a)/1024/5))
                        if [ $in -gt 50 ]
                                then    echo "Downloading at $in KB/s"
                                        echo "Waiting 5 minutes..."
                                        echo ""
                                        count=0
                                        sleep 300  #caffeinate -t 300 before
                                else
                                        count=$((count+1))
                                        echo "Wait $count of 5... (1 minute)"
                                        echo ""
                                        sleep 60 #caffeinate -t 60 before
                        fi
                done
        echo ""
        echo "LET'S SLEEP"
        echo ""
        killall caffeinate
}
This way there's always a caffeinate process running and it's stopped when the program ends.

Now working like a charm.
__________________
Grey computer which does complicated stuff.
Cell phone that can talk to me. (Seriously).
"Fancy quote from dead guy"
dasx is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Caffeinate doesn't work inyourface OS X Mavericks (10.9) 2 Apr 1, 2014 08:52 AM
changed bash to tcsh in gui MAC OS 10.5.8 how to chage back to bash? Mac10.5.8USER OS X 0 Feb 3, 2014 04:45 PM
While loop help - Bash Script BerGaur OS X 10.8 Mountain Lion 2 Dec 24, 2012 02:55 PM
first script/autamator script feed back zyr123 Mac Programming 1 Jul 17, 2012 10:00 AM
Assistance with bash script, read from file djjasonsa Mac Programming 1 Jun 4, 2012 07:52 AM

Forum Jump

All times are GMT -5. The time now is 02:35 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC