Running a script through self service NOT as root

pchrichard
Contributor

I'm running the following script by hand in Terminal which works successfully to mount our user's homedrive:

filteredLDAP=$(ldapsearch -LLL -x -h our.domain -b "OU=People,DC=mds,DC=ad,DC=dur,DC=ac,DC=uk" "(sAMAccountName=$USER)" homeDirectory | sed 's/[^:]*: //;2q;d' | sed 's////g')
connTYPE=$"smb:"
connCONCAT=$"$connTYPE$filteredLDAP"
open $connCONCAT

When I add the script to self-service, nothing happens - I think it's because the script is being run as root and not the current user. Is that the case, and if so, how can I amend to run successfully?

Thanks.

1 ACCEPTED SOLUTION

cubandave
Contributor

You can run actions as the user with the command below.

sudo -u username - H

Something like this might work.

#!/bin/sh
loggedInUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`

filteredLDAP=$(sudo -u "$loggedInUser" -H ldapsearch -LLL -x -h our.domain -b "OU=People,DC=mds,DC=ad,DC=dur,DC=ac,DC=uk" "(sAMAccountName=$USER)" homeDirectory | sed 's/[^:]*: //;2q;d' | sed 's////g')
connTYPE=$"smb:"
connCONCAT=$"$connTYPE$filteredLDAP"
sudo -u "$loggedInUser" -H open "$connCONCAT"

View solution in original post

3 REPLIES 3

cubandave
Contributor

You can run actions as the user with the command below.

sudo -u username - H

Something like this might work.

#!/bin/sh
loggedInUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`

filteredLDAP=$(sudo -u "$loggedInUser" -H ldapsearch -LLL -x -h our.domain -b "OU=People,DC=mds,DC=ad,DC=dur,DC=ac,DC=uk" "(sAMAccountName=$USER)" homeDirectory | sed 's/[^:]*: //;2q;d' | sed 's////g')
connTYPE=$"smb:"
connCONCAT=$"$connTYPE$filteredLDAP"
sudo -u "$loggedInUser" -H open "$connCONCAT"

pchrichard
Contributor

thanks, this is working now

cubandave
Contributor

Can you mark it as answered please? @pchrichard