diff --git a/setup/scripts/l3d b/setup/scripts/l3d
index e576d0175f3221ce10be22b8009f07f981f76ccb..72f5fadbf804a3664e3c1c0285a27aa00aabd65b 100755
--- a/setup/scripts/l3d
+++ b/setup/scripts/l3d
@@ -1,12 +1,25 @@
 #!/bin/bash
 
-SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
-L3DHOSTOS="$(uname -s)"
 SSHAUTH=${SSH_AUTH_SOCK}
 SSHAUTHSOCK=/ssh-agent
+L3DHOSTOS="$(uname -s)"
 case "${L3DHOSTOS}" in
     Darwin*)
+      if ! which greadlink >/dev/null; then
+        # This only works if no symlinks are involved.
+        SCRIPTPATH="$(cd "$(dirname "$0")" && pwd -p)"
+      else
+        # This works on MacOS when coreutils is installed.
+        SCRIPTPATH="$(dirname "$(greadlink -f "$0")")"
+      fi
       ${SCRIPTPATH}/prepareMac4L3d
+      if [[ $? > 0 ]]; then
+        exit $?
+      fi
+      ;;
+    *)
+      # This works in most cases, i.e. on Linux.
+      SCRIPTPATH="$(dirname "$(readlink -f "$0")")"
 esac
 
 ID=$(docker container ls --all -q -f name=^l3drun$)
diff --git a/setup/scripts/prepareMac4L3d b/setup/scripts/prepareMac4L3d
index 966971f8edbb93328ed2fe695a24036c0d520744..d381eebd14500de300901af86197e744a464fe49 100755
--- a/setup/scripts/prepareMac4L3d
+++ b/setup/scripts/prepareMac4L3d
@@ -2,9 +2,9 @@
 
 # Credit: https://github.com/mariusgrigaitis/docker-mac-ssh-auth-sock
 
-SOCATID=$(docker container ls --all -q -f name=^l3d_socat$)
+SOCATID=$(docker container ls --all -q -f name=l3d_socat)
 if [[ -n ${SOCATID} ]]; then
-  return
+  exit 0
 fi
 
 if ! which socat >/dev/null; then
@@ -75,4 +75,4 @@ echo "Starting socket proxy."
 #
 # This is not really reliable because forwarding input/output over stdin/stdout does not allow for multiple communications
 # at the same time. It fails when doing multiple connections to $SSH_AUTH_SOCK at the same time.
-exec socat "EXEC:\"docker run -i -d --rm --restart unless-stopped --name l3d_socat -v $(dirname $SSH_AUTH_SOCK):$(dirname $SSH_AUTH_SOCK) alpine/socat UNIX-LISTEN:$SSH_AUTH_SOCK,reuseaddr,fork -\"" "EXEC:\"socat - UNIX:${SSH_AUTH_SOCK}\""
+exec socat "EXEC:\"docker run -i --rm --name l3d_socat -v $(dirname $SSH_AUTH_SOCK):$(dirname $SSH_AUTH_SOCK) alpine/socat UNIX-LISTEN:$SSH_AUTH_SOCK,reuseaddr,fork -\"" "EXEC:\"socat - UNIX:${SSH_AUTH_SOCK}\"" &