Automating Script Execution

But why run and update Xplanet manually, when you can automate.

This section, like running through the marker file set up is optional. You’ll be prompted to move forward and have the opportunity to exit out of the setup.

Property Lists to Launch on Boot and Update (plist)

Apple introduced launchd as an alternative (replacement?) to cron. launchd is much more robust and powerful than cron, but cron is dead simple to use.  Both these utilities act as a time-based job schedulers to run commands or shell scripts.

I’ll walk through an example of a plist below.  In this case, it’s the plist to update the quake marker.

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

The plist header.

  <key>Label</key>
  <string>local.xplanet.quake</string>

The label should be the name of the plist – minus the “.plist” extension – file.  These must match.

  <key>WorkingDirectory</key>
  <string>/Users/<USERNAME>/.xplanet</string>

Define the working directory the script should run in. We unfortunately cannot reference $USER in plist files to make it automatic.

  <key>ProgramArguments</key>
  <array>
    <string>./config/scripts/xplanet.sh</string>
    <string>quake</string>
  </array>

Tell the plist which script to execute and what, if any, arguments to pass to it.

  <key>StartCalendarInterval</key>

This tells macOS to run, not upon login, but at specified time periods.

  <array>
  <dict>
    <key>Minute</key>
    <integer>19</integer>
  </dict>
  <dict>
    <key>Minute</key>
    <integer>39</integer>
  </dict>
  <dict>
    <key>Minute</key>
    <integer>59</integer>
  </dict>
  </array>

This tells the script to run the 19th, 39th, and 59th minute of every hour.  You can also combine, for example, hour and minute times.  One key would define the hour to run and another would define the minute during that hour to run, i.e., 10:11 am.  You can see this in some of the plists in the archive I made available.

  <key>StandardOutPath</key>
  <string>./logs/xplanet.log</string>
  <key>StandardErrorPath</key>
  <string>./logs/xplanet-quake.log</string>

One of Apple’s launchd most powerful offerings is the ability to define specific files to capture the output of the terminal commands.  Here we define the files for the standard out path and capture any errors in the standard error path.

</dict>
</plist>

These tags mark the end of the plist.

Back to the Setup

sed -i '' "s/<USERNAME>/$USER/" $XPLANET_CONFIG/scripts/plist/*.plist
mkdir -p /Users/$USER/Library/LaunchAgents
cd /Users/$USER/Library/LaunchAgents
for plist_script in $PLISTS; do
cp $XPLANET_CONFIG/scripts/plist/local.xplanet.$plist_script.plist .
launchctl load -w local.xplanet.$plist_script.plist
launchctl start local.xplanet.$plist_script
done

Let’s go back to where we left the setup script and review the last section. We use sed again to edit the plist to match the username since plists do not support using environment variables like $USER. This must to be hardcoded in the plist file.

The relevant plist files are then copied to the LaunchAgents directory in your home directory. The last step is to make macOS aware of the additions and start Xplanet.

You may once again see Apple’s security dialog requesting access to the desktop. Be sure to accept. This should be the final time you see this.

This completes the setup. From here on, Xplanet will start upon login, the monthly earth map will update at the beginning of each month, and the marker files (and cloud map if subscribed) kept up to date periodically using TotalMarker. This is all driven through xplanet.sh as described before.

3 Responses to Automating Script Execution

Leave a Reply to Vic Olivas Cancel reply

Your email address will not be published. Required fields are marked *