mercoledì 18 febbraio 2015

[Mac OS X] keep a shared disk mounted after a network failure

I have several WD NAS with several shares. And I just need them mounted!

Have them available on system startup is pretty easy:

  • mount them on the Finder with different flavors
    • via sidebar
    • via CMD+K
      • smb://192.168.1.101 or better cifs://192.168.1.101
      • afp://192.168.1.101
      • ftp://192.168.1.101
  • open you SYSTEM PREFERENCES (apple menu on the left upper corner)
  • open your USERS PREFERENCES and go to the LOGIN ITEMS tab
  • drag and drop your mounted drives
  • don't forget to check the checkbox on the left where your drives are listed
But what if you have a network failure, wake on sleep etc?
  • Go to / HARD DISK / Applications / Utilities
  • you find the "AppleScript Editor" or "Script Editor" for the most recent systems like Yosemite
  • paste this:
on idle 
  tell application "Finder" 
    set isConnected to disk "FILM" exists 
    set isConnected2 to disk "MP3" exists 
  end tell 
  if isConnected = false then 
    try 
mount volume "FILM" on server "192.168.1.101" 
    end try 
  end if 
  if isConnected2 = false then 
try 
mount volume "FILM" on server "192.168.1.102"
    end try 
end if 
  return 5 
end idle

  • Make sure to edit the IPs and network share names according to yours, I just left you a couple of sample folders and IPs
  • Save as application naming it "checkshares" wherever you like and check the checkbox "leave it opened"
  • Open the terminal and drag & drop your "checkshares" application in order to determine the exact path e.g. /Users/testuser/Desktop/checkshares.app
  • copy the path in the clipboard and delete all
  • from the terminal type "nano Desktop/checkshares.sh", in my example I put all on the user Desktop
  • paste this adapting to your paths and users:
#!/bin/sh
echo 'open /Users/testuser/Desktop/checkshares.app/' | /bin/sh
  • press CTRL+O and CTRL+X to save and quit
  • paste this to the terminal in order to make the script executable:
  • chmod u+x /Users/testuser/Desktop/checkshares.sh
  • paste this to the terminal in order to run checkshares.sh every 5 mins:
  • crontab -e
    */5 * * * * /bin/bash /Users/testuser/Desktop/checkshares.sh
  • press CTRL+O and CTRL+X to save and quit

You can check after 5-6 minutes if you got a new system mail with the command "mail", just to check that everything went fine or if the system generated some alert message. You shouldn't find any email. Everything is ok, so we can reedit crontab:
    crontab -e
    */5 * * * * /bin/bash /Users/testuser/Desktop/checkshares.sh >> /dev/null 2>&1
This command will prevent your crontab line to flood your system mailbox by simply redirecting any output message to NULL device. I rather prefer to add the appended line to a new line and comment out the first line, since I could need to debug something later on.

#########################
UPDATE ON open COMMAND ISSUE

After a while I had the error
LSOpenURLsWithRole() failed with error -600 for the file /Users/testuser/Desktop/checkshares.app

It happened randomically and then generated me thousand messages (one every 60 seconds in my case) until I rebooted the Mac. Infact the "open" command and a bunch of other commands have a long historical issues with the WindowsServer binding: checks after executions, maintaining a resilient session keeping, etc etc.

Googling around didn't help but gave me the inspiration to find this issue and a proper solution.
According to Apple, the 2 schedulers are crontab and launchd, but the second is the preferred one:
https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/ScheduledJobs.html

Starting from 2 nice posts and a little app, which consistently helped me to write a syntactically correct file, I wrote my own PLIST and everything was fine.

Create this file (remember that the name NetworkShares must be unique):
/Users/lost/Library/LaunchAgents/NetworkShares.plist

Paste this:
<?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>NetworkShares</string>
        <key>ProgramArguments</key>
        <array>
                <string>/usr/bin/open</string>
                <string>/Users/testuser/Desktop/checkshares.app</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StartInterval</key>
        <integer>60</integer>
</dict>

</plist>

I also made use of this app http://www.soma-zone.com/LaunchControl/ which enabled me to discover that the minimum seconds are 10 and not the 5 I initially put. I opted for 60 in order to stay safe.

The useful posts are these:

martedì 3 febbraio 2015

[mysql] Enable InnoDB support in MySQL (Unknown table engine ‘InnoDB’)

I converted MyISAM tables to InnoDB engine via PHPmyadmin.

The result was the warning Unknown table engine ‘InnoDB’ and my data were gone!

No problem:

service mysqld stop
rm -f /var/lib/mysql/ib_logfile*
service mysqld start