Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Run script daily in background

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

Posted on Jun 7, 2013 10:35 AM

Reply
18 replies

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.

Jun 8, 2013 2:24 PM in response to Topher Kessler

Topher that sounds more like what im after.

Below is the plist file I created (so the script should run every 30 mins). Does it seem correct? Also is there a way I can create a log file recording evertime it runs so I can make sure it working correctly?


<?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>/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>

</dict>

</plist>

Jun 8, 2013 4:17 PM in response to Topher Kessler

Topher, my plist file will not load.

After doing some research I have updated my plist file to follows:


<?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>/Users/jamesrydings/Documents/FTP Backup Scripts/Backup two.app</string>

</array>

<key>StartInterval</key>

<integer>86400</integer>

</dict>

</plist>



However it still doesnt work. I tried the following launch command:

launchctl load /Users/jamesrydings/Library/LaunchAgents/com.jamesrydings.AutoBackup.plist

launchctl: no plist was returned for: /Users/jamesrydings/Library/LaunchAgents/com.jamesrydings.AutoBackup.plist

launchctl: no plist was returned for: /Users/jamesrydings/Library/LaunchAgents/com.jamesrydings.AutoBackup.plist

nothing found to load


I also tried the following command:

sudo launchctl load -w -F /Users/jamesrydings/Library/LaunchAgents/com.jamesrydings.AutoBackup.plist

launchctl: no plist was returned for: /Users/jamesrydings/Library/LaunchAgents/com.jamesrydings.AutoBackup.plist

launchctl: no plist was returned for: /Users/jamesrydings/Library/LaunchAgents/com.jamesrydings.AutoBackup.plist

nothing found to load


I then tried this:

plutil -lint /Users/jamesrydings/Library/LaunchAgents/com.jamesrydings.AutoBackup.plist

/Users/jamesrydings/Library/LaunchAgents/com.jamesrydings.AutoBackup.plist: Unexpected character { at line 1


There is no { character on line 1. Any thoughts?

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.

Jun 8, 2013 9:11 PM in response to James Rydings

To tackle this problem, try creating the plist using a plain text editor. Perhaps TextEdit is showing some odd problem, so for now use a Terminal-based editor like pico/nano. Here's how to do it:


1. Delete the current Launch Agent file you created.


2. Open the Terminal and run the following command:


pico ~/Library/LaunchAgents/com.jamesrydings.AutoBackup.plist


3. Copy and paste the plist contents from my latest post (above this one) to the Terminal.


4. Press Control-O followed by Control-X to save and exit (not Command).


When done, log out and back in to activate the agent, or you can use the following commands to load, start, and unload it, respectively (for testing purposes):


launchctl load ~/Library/LaunchAgents/com.jamesrydings.AutoBackup.plist


launchctl start com.jamesrydings.AutoBackup.plist


launchctl unload ~/Library/LaunchAgents/com.jamesrydings.AutoBackup.plist

Run script daily in background

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.