Hi All,
I've also needed this, but for me i had to amend the script.. we have 2 VM's called: Template 10.7.3 040512 & Template Win7 32BIT 040512
Amended script below:
user=`ls -la /dev/console | cut -d " " -f 4`
IFS=$'
'
for pvm_file in $( ls "/Users/Shared/Parallels" | grep .pvm );
do
sudo -u $user prlctl register "/Users/Shared/Parallels/${pvm_file}" --preserve-uuid no
echo "Registered ${pvm_file}..."
done
unset IFS
exit 0
oh & ty for this!
don't parse ls
http://mywiki.wooledge.org/ParsingLs
#!/bin/bash
user=$(stat -f %Su /dev/console)
while read -r -d $'' pvm_file; do
su "${user}" -c prlctl register "${pvm_file}" --preserve-uuid no
echo "Registered ${pvm_file}..."
done< <(mdfind -onlyin /Users/Shared kMDItemContentType = "com.parallels.vm.vmpackage" -0)
# or done< <(find /Users/Shared -name "*.pvm" -print0)
Hi, I'm trying to set up a new lab iMac image whereby our students can log in with their AD accounts and get my custom log in user template. But for some reason I can't get these suggestions to work properly.
Specifically, I'm trying to use a LaunchAgent to run a script that uses DockUtil to add a Downloads folder to the custom dock, but it keeps failing, seemingly on the sudo command given here.
This is the LaunchAgent .plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.user.loginscript</string>
<key>Program</key>
<string>/usr/local/bin/UNE_Add_Dock_Icons.sh</string>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
This is the script:
#!/bin/bash
user=`ls -la /dev/console | cut -d " " -f 4`
sudo -u $user /usr/local/bin/dockutil --add /Users/$user/Downloads --no-restart
killall cfprefsd
killall Dock
exit 0
I know I could just use a similar approach to add all my custom apps to the dock, but the script would still need to work!
Here I am running the code manually in terminal as root
sh-3.2# user=`ls -la /dev/console | cut -d " " -f 4`
sh-3.2# echo $user
jtremblay3
sh-3.2# sudo -u $user /usr/local/bin/dockutil --add /Users/$user/Downloads --no-restart
/var/root/Library/Preferences/com.apple.dock.plist does not seem to be a home directory or a dock plist
So it seems the user= code does return the console user, but for some reason sudo -u $user still runs as root. I'm at a loss there.
UPDATE: OK, I don't know why this works, but instead of putting the .plist in /Library/LaunchAgents, I put it in the User Template/Library/LaunchAgents so all new users will get it in their home folder, and I added a sleep 5 command right after the dock util.
@kboissonneault LaunchAgents run as the user, so no need to do the sudo -u or try & find the username.
IIRC, newer builds of dockutil are cfprefsd aware, so no need to kill it, maybe just kill the dock or leave the restart in.
Also, I'd remove the exit 0 as that will show the script as successful even when not.
So something like.
#!/bin/bash
/usr/local/bin/dockutil --add ~/Downloads