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" "">
<plist version="1.0">
		<string>/Users/barry/Music/iTunes Library Extras.itdb</string>

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/; 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/ every 60 seconds:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">

Now, to create the scripts. 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.


# 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

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

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

# 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.

# Second connections

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

# 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"
	if [ -d "$VOL1" ]; then
		syslog -s -l Error "$PROGNAME: $VOL1 already exists. Not trying to mount it."
		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"
	if [ -d "$VOL2" ]; then
		syslog -s -l Error "$PROGNAME: $VOL2 already exists. Not trying to mount it."
		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"

The final script is the one that runs every few minutes, 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.


# 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 ""`

if [ $ITUNES_PROCESS_COUNT -gt 0 ]; then
   `dirname $0`/

I made these scripts accessible only to root with chmod 700 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 | # ]