Mounting volumes in the background

Hello I have been running this script in FileMaker Pro for years now and it has been quite reliable.

I now want to make my process more efficient. I am then starting to run filemaker script on the server than rather on the client.

However, for some of the filemaker scripts a NAS (Synology DS214+) called “FARAMIR” has the be mounted. Up to now there was no issues because those scripts ran on the client.

In running FileMaker server I only have to turn on my Mac mini and the server becomes remotely accessible to all clients. I do not have to logon onto an account and everything for filemaker server to be remotely accessible.

There is an exception, the NAS is not mounted.

Is there a way with an applescript for it to mount the NAS without having to log on? What about super user, would that be the way to go?

With regards!

Thanks


set cancellingscript to "NO"
set mountedFARAMIR to ""
repeat until mountedFARAMIR contains "MOUNTED"
	try
		set mountedERROR to "NO"
		set theping to (do shell script "ping -c 1 -t 1 192.10.5.63")
	on error
		display dialog "There was a problem connecting to the file server. FARAMIR does not seem to be available at this time. Verify if the server was started and try again" with icon stop buttons {"Cancel", "Keep trying..."}
		if button returned of result is "Cancel" then
			set cancellingscript to "YES"
		end if
		set mountedERROR to "YES"
		delay 5
	end try
	if mountedERROR is equal to "NO" or cancellingscript is equal to "YES" then
		set mountedFARAMIR to "MOUNTED"
	end if
end repeat
if cancellingscript is equal to "NO" then
	tell application "Finder"
		set mounted_disks to list disks
		--display dialog result as string
		if mounted_disks does not contain "Data" then
			try
				mount volume "afp://FARAMIR._afpovertcp._tcp.local/Data"
			on error errMSG number errorNumber
				display dialog ("There was a problem mounting FARAMIR/DATA : " & errorNumber as text) & ", please notify your system administrator"
				return
			end try
		end if
	end tell
	delay 3
	
end if

The button “Cancel” is reserved by the display dialog command to have special behaviour - immediately interrupting of the script. Therefore, you can’t get the result of the display dialog command when using exactly “Cancel”. To avoid this behaviour you can do 2 things: 1) set other name, for example: “cancel”, “CANCEL”, “canCel”, “Cancell” only not “Cancel”. 2) set display dialog into 1 second try block, trying to cat the “User cancelled” error.

Because your display dialog is already into one other try block, I advise you to use slightly different name. So as not to create unnecessary confusion.

With 2nd method you should do something like this:


set cancellingscript to "NO"
set mountedFARAMIR to ""
repeat until mountedFARAMIR contains "MOUNTED"
	try
		set mountedERROR to "NO"
		set theping to (do shell script "ping -c 1 -t 1 192.10.5.63")
	on error
		try
			display dialog "There was a problem connecting to the file server. FARAMIR does not seem to be available at this time. Verify if the server was started and try again" with icon stop buttons {"Cancel", "Keep trying..."}
		on error
			set cancellingscript to "YES"
		end try
		set mountedERROR to "YES"
		delay 5
	end try
	if mountedERROR is equal to "NO" or cancellingscript is equal to "YES" then
		set mountedFARAMIR to "MOUNTED"
	end if
end repeat
if cancellingscript is equal to "NO" then
	tell application "Finder"
		set mounted_disks to list disks
		--display dialog result as string
		if mounted_disks does not contain "Data" then
			try
				mount volume "afp://FARAMIR._afpovertcp._tcp.local/Data"
			on error errMSG number errorNumber
				display dialog ("There was a problem mounting FARAMIR/DATA : " & errorNumber as text) & ", please notify your system administrator"
				return
			end try
		end if
	end tell
	delay 3
	
end if

NOTE: I see a lot of redundant code here (several checks for the same condition, extra variables …). After solving the main problem, you should consider optimizing the code.

Updated script.
Removed unnecessary, added necessary. But I couldn’t go any further because the server is offline.


set aMessage to "There was a problem connecting to the file server.
FARAMIR does not seem to be available at this time.
Keep Trying will Verify if the server was started."
set {serverAvailable, buttonPressed} to {"NO", "NO"}

repeat while serverAvailable is "NO"
	try
		set serverAvailable to "YES"
		do shell script "ping -c 1 -t 1 192.10.5.63"
	on error
		if buttonPressed is "NO" then
			display dialog aMessage with icon stop buttons {"CANCEL", "KEEP TRYING..."}
			set buttonPressed to button returned of result
			if buttonPressed is "CANCEL" then
				display dialog "The script cancelled by the user"
				return
			end if
		end if
		set serverAvailable to "NO"
		delay 5
	end try
end repeat

tell application "Finder"
	set mounted_disks to list disks
	--display dialog result as string
	if mounted_disks does not contain "Data" then
		try
			mount volume "afp://FARAMIR._afpovertcp._tcp.local/Data"
		on error errMSG number errorNumber
			display dialog ("There was a problem mounting FARAMIR/DATA : " & errorNumber as text) & ", please notify your system administrator"
			return
		end try
	end if
end tell

Thanks KniazidisR for your reply.

I am sorry for the confusion. The issue here is more the ability to run the script in the background. I.e without logging onto a user account. In running the script in the background there are no user interaction being possible.

I’ve just removed from the script all lines where there was some user interaction. From the script the NAS is found (ping) and Data can be mounted. This implies there would be a sequence in the order the computer and the NAS is turned on.

First, I would start the NAS and wait until the light is no longer flashing;
Two, I would then start the MacMini without logging in.

Then the script to mount the external drive is to be performed in the background.

Is there a way with an applescript for it to mount the NAS without having to log on? Is there a way for the applescript down below to be run in the background? What about super user, would that be the way to go?

try
	do shell script "ping -c 1 -t 1 192.10.5.63"
on error
	return
end try

tell application "Finder"
	set mounted_disks to list disks
	if mounted_disks does not contain "Data" then
		try
			mount volume "afp://FARAMIR._afpovertcp._tcp.local/Data"
		on error
			return
		end try
	end if
end tell

I think mounting drives is best to do with script in /etc

ex.

save that line to your auto_smb inside /etc on your macmini.

After that I do not think you need to login to your macmini

http://www.csd.uwo.ca/~amulder/HowTo/smb-mounts-auto/

EDIT: 13/2 - 2020
If any has problem with connection or performance of using smb2 or smb3 should read the
link below from Apple Suggestions Published Date: January 10, 2019
https://support.apple.com/en-us/HT205926

If you didn’t know Apple have deprecated afp, in favour for smb it’s faster on mac.

Thanks Fredrik71.

Wow, this is exactly what I was looking for. This is greatly appreciated, I will be testing it on next week.

Also I did not about apple afp vs smb. I have been using the Go + Connect to server command and they only offer afp as a choice. Maybe it’s because I am using Mojave.

I will also look into this.

With regards!

In Go + Connect… it’s not always logic.

But you could try… cifs://YOUR_NETWORK_ADDRESS…

people who have problem with smb:// use cifs:// instead…

Most NAS server has support for at least this protocols… smb, nfs and sometimes afp…
So check your setup in your NAS it’s not Mac setting.

Hello Fredrik71

I am trying to understand the command line.

On my system there is no Share folder but there is a Shared folder. Would that be the same ?

What does the MyDrive stand/used for ?

In looking in the old afp command and the smb command you are recommending I would then substitue the NAS_DRIVE with FARAMIR and /directory_path with /Data

What do you think in adding : soft,noowners,noatime,nosuid part of the -fstype

The command would then look like this, :

does it make sense? At the moment I really do not understand the statement “/Users/Shared/MyDrive”

Also in going within the etc folder, I’ve noticed there is no « auto_smb » file.

Should I add in the auto_master the following ?

Network Shares

/- auto_smb -nosuid,noowners

and then create the auto_smb file with the command line -fstype=smbfs,soft… ?

With regards!

Thanks!

The NAS is your server
Macmini is your client.

If you do not have auto_smb file in your /etc then you create one, it’s a simple text file.

The setting you do inside /etc/auto_smb
Will make your macmini to know the locate of directory_path, username and password to connect with your NAS.

The setting after -fstype=afp
you will find username, password and IP or SERVERNAME
To know the username and password and IP you could get from the settings in your NAS.

Now we know the settings for NAS.

But we also need to mount it to the Macmini.

The setting before -fstype=afp
Is the path on your Macmini there you like it to be.

ex.

Lets say you have create a share on your NAS with the directory name MediaOfAwsomeFilms
You like that directory to be inside your Home/Movies

Home=(/Users/YOUR_USERNAME)

When you start your Macmini and go to your directory Movies
You will have a folder with the name ‘MediaOFAwsomeFilms’

/Users/your_user_name/Movies -fstype=afp afp://your_username:your_password@NAS_NAME/DIRECTORY_THAT_YOUR_NAS_USE_TO_SHARE

DIRECTORY_THAT_YOUR_NAS_USE_TO_SHARE= The NAS also have directory structure and every
NAS server do it different, if you create a custom directory on your NAS it will not be in your root.
Many unix system use a folder ‘Media’ but it could be anything. Some maybe use /usr/local/media

But you could connect to the server with finder and from that know the last part of the setting
for your auto_smb

Maybe it’s enough to type your custom folder on your NAS after NAS_NAME

So say your NAS use /usr/local/media Sharing setup.

If you create a custom folder… it will be inside /usr/local/media/name_of_your_custom_folder

So how does that look in on Mac you wonder…

If you type cd /Users

You will se a folder with the name Shared

This is a folder we could use to share files to all users. If you like to do that…
If it’s only you… and you always login to your account I guess you could share to your account instead of /Users/Shared

I hope that make sense :slight_smile:

Here is other site that use ‘afp’
https://blog.netnerds.net/2013/10/mounting-nas-volumes-in-mac-os-x-the-right-way/

MANY THANKS for your help, it is GREATLY appreciated.

On my NAS named FARAMIR, the directory structure is the following:
/Data/ZVinformatique/CompétitionCourante/01/mp4files.mp4
/WebMediaFile/WebDAV/clip/mp4files.mp4
/CompétitionPrécédente/mp4files.mp4

When accessing the files I always refer to :
/Volumes/Data/…
/Volumes/WebMediaFile/…
/Volumes/CompétitionPrécédente/…

For my programs to work I need whatever is found after Volumes stays the same. Doing the following would that make sense.

/Users/Shared/[YourDirectory] -fstype=smbfs ://[username]:[password]@FARAMIR/Volumes/Data

/Users/Shared/[YourDirectory] -fstype=smbfs [colon]//[username]:[password]@FARAMIR/Volumes/Data

[colon] = :

The options you have
soft,noowners,noatime,nosuid

or

soft,noowners,noatime,nosuid,rw

rw = read & write

if the option do not work for you, leave them out…

====

you also need

colon 2 forward slash

://

before username, see the example above, the problem is when I type it here I get emoji

Thanks again.

I had the same issue you had with the emoj, I clicked “never show smile as icons for this post”

Without the option

the [YourDirectory] should they be different one for /Data and the other one for /CompétitionPrécédente. My guess would be that they have to be different. Am I correct?

With the option

Yes, about the options do not know what they do or if you need them.

I do know it works without them at least on my system.

Here are the auto_master and the auto_smb files stored in /etc

/etc/auto_master

/etc/auto_smb

Is there a way to know if those are being executed?
Is there a way to test it without having to restart the macmini?

Where could I find more about this type pf programming?

Thanks!

Thanks again!

I am a beginner in this type of scripting. I know this is not applescript, neither I believe it is shell scripting. How do we call this type of scripting?

As per the log,

I’ve looked into Console + System Reports, User Reports, system.log, /Library/Logs, /var/log and do not find any statement about mounting successfully the NAS or not. I did not find anything there?

I did cmd + v upon startup and the log started to display. It went so fast that it was difficult to see if auto_smb was executed. I then logged in under my account and under terminal typed the following “sudo dmesg” command. I’ve copied and pasted the log in a textedit document. Doing a search on the command line declared in auto_smb I did not find anything.

In looking at the two posted links in more details I saw instructions on “How to configure your MAC to automatically mount an SMD share as needed”. Within the instructions there is a bunch of sudo command. I did not execute any of the sudo command. When updating the auto_master and creating the auto_smb file I’ve :

1 - went directly into the /etc folder;
2 - copied /etc/auto_master and pasted it in desktop;
3 - textedit auto_master to add the auto_smb statement;
4 - pasted another copy of auto_master, renamed auto_master to auto_smb;
5 - recorded in auto_smb the mount statement;
6 - pasted/replaced the desktop auto_master in /etc;
7 - pasted the desktop auto_smb in /etc.

Should I’ve been using the sudo command instead?

MANY THANKS AGAIN!

  1. Your steps above is the 1st manual way to change etc contents (editing it in the TextEdit).
  2. Other manual way is using Terminal window + sudo.
  3. 3rd way. To do all at the place using AppleScript, instead of sudo you should use with administrator privileges statement.

For example, instead of $ sudo touch /etc/auto_afp (in Teminal window), you should use this:

do shell script "touch /etc/auto_afp" with administrator privileges

The directory /etc belong to the root… so ‘how do I know that’

if you type: ls -al /etc

You will see… ‘root’ that is the owner, ‘wheel’ is the group.
The wheel group is a group which limits the number of people who are able to su to root.

Normal user account without administrator access will not be able to write but maybe read.

To edit /etc/auto_smb you could use sudo… if your account have administrator access.
The simples way is to login as Administrator and use vi, nano… editor to change that file.

ps. I have only told you to change the auto_smb or create one, not changing the auto_master

If you change the auto_master you maybe break something else.

Please note, I sincerely appreciate your help. This is difficulty for me because I trying to do something I require but for which I am not familiar with this scripting.

Confirmed!

I still have the original. In this link http://www.csd.uwo.ca/~amulder/HowTo/smb-mounts-auto/

$ sudo vi /etc/auto_master

Add the line “/- auto_smb” to that file.
$ sudo touch /etc/auto_smb
$ sudo vi /etc/auto_smb
$ sudo chmod 600 /etc/auto_smb

To me performing

was the same then doing

Peforming

was the same than doing

I do not know what the

is doing.

Then, if better I could go back to the original auto_master and perform all of the sudo commands as describe in the link http://www.csd.uwo.ca/~amulder/HowTo/smb-mounts-auto/

Do you recommend for me to start all over and do the sudo commands as described in the link above?

_