Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Ten different ways to connect to servers' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Ten different ways to connect to servers
Authored by: jamietshaw on Apr 08, '10 02:18:10PM
Very useful. There is yet another way: run scripts with launchd. Our music is stored on a server and I wanted to do the following things:
  • Automatically connect to the server upon login without a Finder window appearing (as happens with System Preferences > Accounts > Login Items), and without using a third party program
  • Check server connection when iTunes is started, and connect if necessary
  • Check and connect every few minutes while iTunes is running
By using two launchd items and two scripts, this is possible. First, the launchd items. Lingon can be used to set these up with a GUI, otherwise create a text file with content such as:
<?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.barrytshaw.ConnectToNAS</string>
	<key>ProgramArguments</key>
	<array>
		<string>/Users/Shared/ConnectToNASlaunchd.sh</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
	<key>WatchPaths</key>
	<array>
		<string>/Users/barry/Music/iTunes Library Extras.itdb</string>
	</array>
</dict>
</plist>

Save in ~/Library/LaunchAgents if specific to a user or /Library/LaunchAgents to run for all users. This 'agent' runs when someone logs in and when the /Users/barry/Music/iTunes Library Extras.itdb is modified - which generally only happens when iTunes starts up. It doesn't seem possible to monitor the main iTunes Library database, perhaps because of its lack of filename. The agent expects to run a shell script /Users/Shared/ConnectToNASlaunchd.sh; this will actually do the connecting and will be written later.

One further launchd agent can be created to run every few minutes. In the example below, it runs a script called /Users/Shared/ConnectToNASifITunesRunning.sh every 60 seconds:

<?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.barrytshaw.ConnectToNASiTunesCheck</string>
	<key>ProgramArguments</key>
	<array>
		<string>/Users/Shared/ConnectToNASifITunesRunning.sh</string>
	</array>
	<key>StartInterval</key>
	<integer>60</integer>
</dict>
</plist>

Now, to create the scripts. ConnectToNASlaunchd.sh does the connecting to the server. In this example, it actually mounts two sharepoints with SMB. It also contains commands for AFP connections, commented out.


#!/bin/bash

# Program to connect to two sharepoints on a remote server.
# Make sure this script is executable by root.

# Problems: password is hard-coded into this script. No access should
# be given to this script except to admins and root.

PROGNAME="Connect to NAS (barry)"

# IP address or name of server
IP="192.168.0.10"

USERNAME="`whoami`"  # Use "`whoami`" if OS X short username is same as username on server.
PWD="thing"

# No. of secs to stop checking for the IP address
# Give time for server to awake from sleep.
TIMEOUT=10

# First connection: where to mount locally
VOL1="/Volumes/DS Shared"

# First connection: name of remote sharepoint, with spaces converted to %20 etc.
# To see which special characters need converting, connect with Finder's Go > Connect to Server
# and once connected, do Get Info on the server to see the name.
SHARE1="DS%20Shared"

# Second connections
VOL2="/Volumes/web"
SHARE2="web"


# =======================================================================

# Check server actually exists before trying to connect
/sbin/ping -c 1 -t $TIMEOUT $IP &>/dev/null
if [ $? -ne 0 ]; then
	syslog -s -l Error "$PROGNAME: Cannot connect to server $IP"
	exit
else
	if [ -d "$VOL1" ]; then
		syslog -s -l Error "$PROGNAME: $VOL1 already exists. Not trying to mount it."
	else
		mkdir "$VOL1"
		chown $USERNAME "$VOL1"
		chmod 777 "$VOL1"
		syslog -s -l Warning "$PROGNAME: Connecting to NAS for $VOL1"

		# Fortunately sudo isn't required, because it logs commands (incl. the password) in the Console.
		#/sbin/mount -t afp -a -o nodev,nosuid "afp://$USERNAME:$PWD@$IP/$SHARE1" "$VOL1"
		/sbin/mount -t smbfs -o noatime,nodev,nosuid,-d=0777,-f=0777 "//$USERNAME:$PWD@$IP/$SHARE1" "$VOL1"
	fi
	
	if [ -d "$VOL2" ]; then
		syslog -s -l Error "$PROGNAME: $VOL2 already exists. Not trying to mount it."
	else
		mkdir "$VOL2"
		chown $USERNAME "$VOL2"
		chmod 777 "$VOL2"
		syslog -s -l Warning "$PROGNAME: Connecting to NAS for $VOL2"

		#/sbin/mount -t afp -a -o nodev,nosuid "afp://$USERNAME:$PWD@$IP/$SHARE2" "$VOL2"
		/sbin/mount -t smbfs -o noatime,nodev,nosuid,-d=0777,-f=0777 "//$USERNAME:$PWD@$IP/$SHARE2" "$VOL2"
	fi	
	
fi

The final script is the one that runs every few minutes, ConnectToNASifITunesRunning.sh. It checks to see if iTunes is running, and if so runs the other connect script. If iTunes is not running it does nothing, meaning that the server doesn't get woken up from sleep unnecessarily.

#!/bin/sh

# See if iTunes is running. If so, check the server is connected and connect if needed.
# Could be run every few mins from launchd.

# Assumes that iTunes is called iTunes, but doesn't have to be in the Applications folder.
# Can't just search for 'iTunes' in the process list, because 'iTunesHelper' is always running.
ITUNES_PROCESS_COUNT=`ps -A -o comm | grep --count "iTunes.app/Contents/MacOS/iTunes"`

if [ $ITUNES_PROCESS_COUNT -gt 0 ]; then
   `dirname $0`/ConnectToNASlaunchd.sh
fi

I made these scripts accessible only to root with chmod 700 ConnectToNASifITunesRunning.sh etc.

NB In addition, I made the ~/Music/iTunes Media/Music folder an alias to the actual location of the music and locked it. iTunes has an annoying habit of defaulting back to this location if the proper location isnít available.



[ Reply to This | # ]