Launchd Help for the Clueless

Discussion in 'Mac Programming' started by myibookg4, Oct 11, 2013.

  1. myibookg4 macrumors newbie

    Joined:
    Nov 14, 2010
    #1
    Hi all,

    I downloaded Launchd after someone recommended it to me for scheduling repeating tasks...figured $4, big whoop. However now that $4 seems like wasted cash -- I had no idea it required knowledge of writing "plists" and "bash" files and all this other stuff. Lots of threads tell me to buy this or that OTHER program to get it to work. Have I wasted my time/money trying to work with a utility that is beyond my comprehension??

    All I want to do is schedule my wifi to turn off and on at specified times. I have used iCal (Calendar in OSX 10.8.5 -- despite my name I also have a 2013 Macbook Air) to try to do this but (and this was my main reason for wanting to use Launchd) it does not work when the computer is asleep, which for my purposes defeats the whole point. I'm doing this for my own productivity as I work from home.

    I found a script online that would do this for me but it doesn't seem to work with Launchd. All I need to do is have the wifi turn off at 8:30am, on again at 11:15am, off at 7pm, and on again at 8pm. I want it to do this whether the computer is awake or not. Keep in mind I know nothing about writing plist files or what to do with them if I did. I have researched a lot but all instructions online I can find, even ones "for dummies" are really complicated.

    The most helpful thing would be if someone could give me some kind of "blank" form plist file with instructions about where to put it, how to load/unload, etc., or just write the script for me (too much to ask?). I am also open to alternatives.

    Thanks in advance, you guys are awesome.
     
  2. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2

    This is probably not all that helpful, but could you post a link to where you downloaded it?

    I ask because launchd literally runs every program on OSX (it's "process 1" on the system), so it shouldn't need to be downloaded. Want to make sure you didn't download something weird :)
     
  3. myibookg4 thread starter macrumors newbie

    Joined:
    Nov 14, 2010
    #3
    Weird. Well here's where I downloaded it. http://download.cnet.com/LaunchD-Task-Scheduler/3000-2084_4-75939569.html

    Shows how much I know.
     
  4. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    Does the script work when you run it from inside Applescript Editor or if it is a shell script from inside the Terminal? Please post a link to the script or post the code in your reply.

    Have you looked at the FAQ of Launchd task scheduler here
     
  5. myibookg4 thread starter macrumors newbie

    Joined:
    Nov 14, 2010
    #5
    Here is the script:

    --
    -- Toggle Airport Power On and Off
    --
    if (offset of "On" in (do shell script "networksetup -getairportpower en0")) > 0 then
    do shell script "networksetup -setairportpower en0 off"
    else
    do shell script "networksetup -setairportpower en0 on"
    end if


    I found it online somewhere. It definitely works as an applescript, I have not tried it (nor know how) in terminal. Originally I saved it as an application from Applescript Editor and then scheduled it in Calendar to run at certain times, which still works. But (see above) it doesn't seem to work with Launchd and doesn't work when the computer is asleep.

    I will look at the FAQ but in my experience it is usually too technical for me...:)

    ----------

    Update: I looked at the FAQ and was able to run the diagnostic thingy with Console and got this message whenever my script showed up:

    "Job failed to exec(3) for weird reason: 2"

    ...which I have no idea what that means.
     
  6. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #6
  7. myibookg4 thread starter macrumors newbie

    Joined:
    Nov 14, 2010
    #7
    Right. So does anyone know what "weird reason 2" is? :confused:
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    I think your goal is unachievable as described.

    You wrote:
    First, if the computer is asleep, then the wifi is already off. If the computer is asleep, it's also not altering your productivity.

    Second, nothing executes while the computer is asleep. This is an inseparable side-effect of being asleep. To run a script when the computer is asleep, it would have to wake, then run the script, then go back to sleep. This must happen each time that the wifi state changes (on to off, or off to on).

    MacBooks do have the ability to schedule one repeating on/wake event, and one repeating off/sleep event. So a scheduled wakeup is possible. But it would then have to change the time of the next wakeup event. You can see the basic capability in System Preferences > Energy Saver > click the "Schedule..." button at lower left. This is for scheduling one repeating event of each type per day. Your description uses two repeating events of each type, so it's inherently more complex.

    The other difficulty with scheduling a repeating sleep/off event is what it should do if the computer is in use at the time. You said you want to turn wifi off for productivity. I assume this is to stop you from going online, or some such thing. That is, it's an enforcement of some guideline or discipline you've set (or want to set). Now, if the computer is being used at the time of a scheduled sleep/off event, what should it do? Should it force a sleep or off to occur? Should it sense the person using the computer and postpone the sleep/off? How long should it postpone it?

    Since you never explained exactly how turning wifi off and on is intended to alter your productivity, some of the above is based on assumptions.

    You also haven't said whether there are other computers using your wifi network during the times you've listed as "off" periods. If yours is the only computer, then one alternative is to turn off the wireless router, i.e. completely shut down your wireless network. You can buy timed power outlets that turn things off and on at scheduled times every day. They're about $10 ea. at places like Amazon.com. You can also find them at stores like Lowe's, Home Depot, and maybe even Target or Walmart. Amazon search terms timed power outlet or scheduled power outlet. Also try timed power strip.
     
  9. myibookg4 thread starter macrumors newbie

    Joined:
    Nov 14, 2010
    #9

    Thank you for the suggestion of the timed power outlet. That might solve my problem, and it may be what I resort to if I cannot get Launchd to work. However, I think you are mistaken in thinking this is impossible. That is my non-expert opinion but it is based on my understanding of what Launchd is capable of, and indeed what makes it most useful, and I would *prefer* to put my $4 + intellectual elbow grease to work as opposed to throwing in the towel quite yet. (That may change in one or two days though.)

    Any nincompoop knows that if the computer is asleep the wifi is off. I also understand that scripts can't run when the computer is off -- I don't need them to. Launchd, in my understanding, runs any scripts that were not run while asleep immediately upon waking, so that (for example) if the computer went to sleep with the wifi on, and while it was asleep a scheduled task ("turn wifi off") did not run, Launchd would run it upon waking and it would be, for my purposes, as if it *did* run while asleep. I also know that multiple scheduled events will be consolidated into one event, which again for my purposes doesn't matter. The script I have (above) just does the opposite of whatever the situation is, i.e., off if on, on if off.

    Most of the other things you said do not apply because I am not trying to schedule a sleep or wake event. If the computer/wifi is in use at the time, it will simply turn the wifi off (or on) -- which is exactly what I want to happen.

    I didn't think it was relevant, but yes, my reasons for doing this are supplemental to (or maybe in lieu of?) self-discipline. I could go on and on about how it has been so ingrained in my generation, to the point of unconscious reflex, to Google and Google and be perpetually distracted, but that is not the point....considering how much time I'm spending on this maybe I should just do the ****** plug timer. ;) alas I have one of those stubborn personalities, which may also explain why I resisted upgrading from an ibookg4 until this year.

    But seriously I think my goal is achievable. I just need help getting the script I already have, or another script that does the same thing, to run with Launchd. I'll give it a few more days and see if anyone has any ideas.

    Thanks!
     
  10. kryten2, Oct 15, 2013
    Last edited: Oct 16, 2013

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #10
    Here's an example. Perhaps you can adapt it to your needs and try it out. I'm using 4 files. I am not using Airport in the script as you can see. I'm just dumping a text string to a log file named LaunchdWifi-events.log to give you an idea and to show that the two jobs are executing.

    • Applescript Files : WifiOff.scpt and WifiOn.scpt
    • Launchd plist files : com.test.WifiOff.plist and com.test.WifiOn.plist

    com.test.WifiOff.plist :

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    	<key>Label</key>
    	<string>com.test.WifiOff</string>
    	<key>Program</key>
    	<string>/usr/bin/osascript</string>
    	<key>ProgramArguments</key>
    	<array>
    		<string>osascript</string>
    		<string>/Users/kryten/Library/Scripts/WifiOff.scpt</string>
    	</array>
    	<key>StartCalendarInterval</key>
    	<array>
    		<dict>
    			<key>Hour</key>
    			<integer>3</integer>
    			<key>Minute</key>
    			<integer>27</integer>
    		</dict>
    		<dict>
    			<key>Hour</key>
    			<integer>3</integer>
    			<key>Minute</key>
    			<integer>29</integer>
    		</dict>
    	</array>
    </dict>
    </plist>
    
    com.test.WifiOn.plist :

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    	<key>Label</key>
    	<string>com.test.WifiOn</string>
    	<key>Program</key>
    	<string>/usr/bin/osascript</string>
    	<key>ProgramArguments</key>
    	<array>
    		<string>osascript</string>
    		<string>/Users/kryten/Library/Scripts/WifiOn.scpt</string>
    	</array>
    	<key>StartCalendarInterval</key>
    	<array>
    		<dict>
    			<key>Hour</key>
    			<integer>3</integer>
    			<key>Minute</key>
    			<integer>28</integer>
    		</dict>
    		<dict>
    			<key>Hour</key>
    			<integer>3</integer>
    			<key>Minute</key>
    			<integer>30</integer>
    		</dict>
    	</array>
    </dict>
    </plist>
    
    WifiOff.scpt :

    Code:
    set themessage to "The Launchd job WifiOff will set the Airportpower to off."
    
    log_event(themessage)
    
    on log_event(themessage)
    	set theLine to (do shell script ¬
    		"date  +'%Y-%m-%d %H:%M:%S'" as string) ¬
    		& " " & themessage
    	do shell script "echo " & theLine & ¬
    		" >> ~/Desktop/LaunchdWifi-events.log"
    end log_event
    
    WifiOn.scpt :

    Code:
    set themessage to "The Launchd job WifiOn will set the Airportpower to on."
    
    log_event(themessage)
    
    on log_event(themessage)
    	set theLine to (do shell script ¬
    		"date  +'%Y-%m-%d %H:%M:%S'" as string) ¬
    		& " " & themessage
    	do shell script "echo " & theLine & ¬
    		" >> ~/Desktop/LaunchdWifi-events.log"
    end log_event
    
    Contents of LaunchdWifi-events.log :

    2013-10-16 03:27:00 The Launchd job WifiOff will set the Airportpower to off.
    2013-10-16 03:28:00 The Launchd job WifiOn will set the Airportpower to on.
    2013-10-16 03:29:00 The Launchd job WifiOff will set the Airportpower to off.
    2013-10-16 03:30:00 The Launchd job WifiOn will set the Airportpower to on.


    You need to change the ProgramArguments string of the path to the script e.g. /Users/kryten/Library/Scripts/WifiOn.scpt to your correct path where you saved your scripts and also change the StartCalendarInterval's Hour and Minute integers in both plist files.

    You can load your plist files by doing the following in Terminal :

    Code:
    launchctl load /Users/kryten/Documents/com.test.WifiOff.plist
    launchctl load /Users/kryten/Documents/com.test.WifiOn.plist
    
    Also change the paths to where your plist files are.

    Your can check if you plists are loaded by doing the following in Terminal :

    Code:
    launchctl list | grep Wifi
    Note : Tested on Mavericks GM. YMMV. Good luck. No Launchd task scheduler or any kind of helper was used in doing this.
     
  11. myibookg4 thread starter macrumors newbie

    Joined:
    Nov 14, 2010
    #11
    Ok...I will give that a shot. :eek: Thanks!

    Although when I tried to save a .plist file before it said I didn't have permission, then when I changed the permissions, it just didn't save it with no explanation.
     
  12. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #12
    For testing save your .plist files in your Documents folder. When everything works you can move them to your /Users/yourusername/Library/LaunchAgents folder so launchd can find and load them. Before you move them don't forget to unload them eg :

    Code:
    launchctl unload /path/to/your/whatever.plist
     

Share This Page