Skip navigation

Run script daily in background

949 Views 18 Replies Latest reply: Jun 9, 2013 6:44 PM by Topher Kessler RSS
1 2 Previous Next
James Rydings Calculating status...
Currently Being Moderated
Jun 7, 2013 10:35 AM

I created a script that checks for an internet connection, and if present backsup some important files if modified to my server using Transmit. I would like it to run in the background once a day. I currently have it set up to run daily in iCal which works fine, but I now have all these iCal events listed on all my iDevices. Is there another way I can do this?

iMac, Mac OS X (10.7.3), 3.06 ghz, 4gb, 512mb
  • Jeffrey Jones2 Level 6 Level 6 (8,425 points)
    Currently Being Moderated
    Jun 7, 2013 11:40 AM (in response to James Rydings)

    What's wrong with Calendar (formerly iCal)? If you just don't want to see the events, put them in a special calendar then uncheck it so that the events aren't displayed.

  • Linc Davis Level 10 Level 10 (107,955 points)
    Currently Being Moderated
    Jun 7, 2013 12:11 PM (in response to James Rydings)

    Move the event to a calendar that isn't synced with the other devices.

  • Linc Davis Level 10 Level 10 (107,955 points)
    Currently Being Moderated
    Jun 8, 2013 10:21 AM (in response to James Rydings)

    File > New Calendar > On My Mac

  • Barney-15E Level 7 Level 7 (33,410 points)
    Currently Being Moderated
    Jun 8, 2013 10:29 AM (in response to Linc Davis)

    If you don't have the ability to add a calendar to On My Mac, enable the Birthdays calendar in preferences, then select it in the list of calendars. Below that, right-click in the empty space and select New Calendar. It will be created in the "ON MY MAC" section.

  • Topher Kessler Level 6 Level 6 (9,305 points)
    Currently Being Moderated
    Jun 8, 2013 10:49 AM (in response to James Rydings)

    What type of script is it?

     

    To run a script once per day in the background, you can create a launch agent for your account or for the system that will run the script in the background. To do this, create a plain text file with the following contents, and save it with the file extension ".plist" in the LaunchAgents directory (for instance "dailytask.plist").

     

    You can do this by holding the Option key and choosing Library from the Finder's Go menu, and then navigating to the LaunchAgents folder. Keep this folder open so it will be accessible.

     

    Next, open TextEdit and create a new document, then press Shift-Command-T to make it a plain text document, and then copy the following contents to the document and save it to your desktop.

     

    <?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>local.dailytask</string>
              <key>ProgramArguments</key>
              <array>
                        <string>/PATH/TO/SCRIPT/GOES/HERE</string>
              </array>
              <key>QueueDirectories</key>
              <array/>
              <key>StartCalendarInterval</key>
              <dict>
                   <key>Hour</key>
                   <integer>12</integer>
                   <key>Minute</key>
                   <integer>30</integer>
              </dict>
    </dict>
    </plist>

     

    Now change the text that read "PATH/TO/SCRIPT/GOES/HERE" to be the full path to the location of your script, so for instance if your script is called "myscript.sh" and is in your Documents folder, then you would put the following (replacing "username" with the short name of your account--ie, your home folder name):

     

    /Users/username/Documents/myscript.sh

     

    Next locate where it says Hour and Minute, and change these to be the desired time you want the script to run (hours are in 24-hour format).

     

    With these changes made, save the document and then rename it so instead of having ".txt" as the extension, make the extension be ".plist" (confirm the change when prompted). Then copy or move the file from your Desktop to the open "LaunchAgents" folder.

     

    With this setup, now log out and log back into your account, and the system should run the designated script at the appropriate time.

     

    Do keep in mind to use AppleScripts you will need to specify more details instead of just the path to script as I mentioned above, or optionally you can have the path here point to a small secondary shell script that itself will contain a command to run the desired AppleScript or other script.

     

    Lastly, the use of the LaunchAgent folder in your home directory means this script will only load when you log in, and will run under the scope of your user account. If you want it to run for all users or even if the system is at the login window, then you will have to use the global LaunchAgents directory in the Macintosh HD > Library folder.

     

    If you run into any hurdles with this approach, post back here and folks will be glad to help you set it up.

    MacBook Pro, OS X Mountain Lion (10.8.2), 17&quot;, 2.5GHz i7, 16GB RAM, 512GB SSD
  • putnik Level 3 Level 3 (620 points)
    Currently Being Moderated
    Jun 8, 2013 2:49 PM (in response to James Rydings)

    I should think that Time Machine would perform regular backups like this?  You can exclude items not required and keep a regular backup schedule.

  • Linc Davis Level 10 Level 10 (107,955 points)
    Currently Being Moderated
    Jun 8, 2013 4:05 PM (in response to James Rydings)

    That won't work.

  • drdocument Level 4 Level 4 (2,995 points)
    Currently Being Moderated
    Jun 8, 2013 4:16 PM (in response to James Rydings)

    As a possible alternative...

    Have you considered creating a pair of sync'ed folders between your Mac and your server?

    It might be less hassle than a script.

  • putnik Level 3 Level 3 (620 points)
    Currently Being Moderated
    Jun 8, 2013 7:23 PM (in response to James Rydings)

    You can use Time Machine on a network too.  As I said, you can exclude folders you don't want to backup, though once the initial run has completed the backups will be only of changed files.  It has the advantage that you can recover the system from it.

  • Topher Kessler Level 6 Level 6 (9,305 points)
    Currently Being Moderated
    Jun 8, 2013 9:15 PM (in response to James Rydings)

    James,

     

    It appears you are trying to launch an application directly with the launch agent. This can be done using the "open" command in the launch agent in the following manner (changes are in bold):

     

    <?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.jamesrydings.AutoBackup</string>
              <key>ProgramArguments</key>
              <array>
                        <string>open</string>
                        <string>/Users/jamesrydings/Documents/FTP Backup Scripts/Backup two.app</string>
              </array>
              <key>QueueDirectories</key>
              <array/>
              <key>RunAtLoad</key>
              <true/>
              <key>StartInterval</key>
              <integer>1800</integer>
    </dict>
    </plist>

     

    My guess is you created an AppleScript or Automator application, in which case this will run it as you wish on the designated time interval (or calendar time). If the system is asleep or off during this time, then it will queue up the missed instance and run it the next time the system is awake, and then continue at the set interval time.

    MacBook Pro, OS X Mountain Lion (10.8.2), 17&quot;, 2.5GHz i7, 16GB RAM, 512GB SSD
1 2 Previous Next

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.