I have a script to temporarily mount an SMB share and copy some files that get generated for my team.
It works find locally, but fails to mount the remote share when run from cron
. I am not including the script as the commands in question should be evident from the outputs below.
Local operation output:
$ get-reports.sh
Tue Feb 18 16:14:05 EST 2020
+ whoami
gtarsa
+ id
uid=1234567(gtarsa) gid=1234567(#######) groups=1234567(#######),<usual Apple groups>
+ /sbin/umount /Users/gtarsa/reports
+ /sbin/mount -t smbfs //engsrv/reports /Users/gtarsa/reports
+ find /Users/gtarsa/reports -iname '*gtarsa*' -exec cp '{}' /Users/gtarsa/Downloads/reports ';'
+ /sbin/umount /Users/gtarsa/reports
+ set +x
$
Cron result:
$ cat ~/Downloads/reports/copy.log
Tue Feb 18 12:20:00 EST 2020
+ whoami
gtarsa
+ id
uid=1234567(gtarsa) gid=1234567(#######) groups=1234567(#######),<usual Apple groups>
+ /sbin/umount /Users/gtarsa/reports
+ /sbin/mount -t smbfs //engsrv/reports /Users/gtarsa/reports
mount_smbfs: server rejected the connection: Authentication error
+ echo '? Unable to mount Reports Share'
? Unable to mount Reports Share
+ exit 1
$
I have tried explicitly specifying connection credentials (as described in man mount_smbfs
) with no success. I am running script manually when I need it, but would prefer to have it run periodically on its own.
Something is different in the cron
environment from my interactive environment, but I have not been able to discern what. Google searches for answers come up with lots of "How to mount a share from the command line", but not from cron
. Ideas and insight welcome.
Best Answer
Yes, some things are different in the
cron
environment from yourgtarsa
environment. You can check thecron
user environment for yourself using this "recipe".Since you can run your script manually, it seems likely you've set up your Samba passwords and permissions appropriately.
You've chosen not to share your
crontab
entry, but I'vemount
ed andumount
ed my Samba shares from acron
script with lines in mycrontab
like this:This assumes a mountpoint declared (
mkdir
) at/Users/seamus/SmbSrv01
Since the
cron
user's path is not the same asgtarsa
, you should check that you're using full path specifications to everything that's not in thecron
user's PATH... or just everything if you're not sure.Also NOTE: Putting
userid
andpassword
in a script or acrontab
as I've done here is something you should not do. Less insecure is putting theuserid
andpassword
in~/.bash_profile
; e.g.:Add these lines to the bottom of
~/.bash_profile
, and use these environment vars with themount
command in your script. Before you run the script, reload~/.bash_profile
to pick up the environment variables you've just declared:<redundant>
You've chosen not to share yourcrontab
entry</redundant>
, but if you've not already done so you could help yourself by directingstderr
from your script to a file for post-mortem analysis. In yourcrontab
:will capture anything output to
stderr
in the file copy.logFinally, if you're running your script under the
@reboot
spec, you should probably put asleep
command before you try tomount
.If none of this helps, please edit your Q to add your
crontab
entries, and as much of your script as possible and relevant.