PDA

View Full Version : [Resolved] Logout hook for specific users | Delete and recreate Library + Ownership




h1068
Jun 13, 2012, 10:50 PM
Here is the list of users I need the script to apply to:

users/
gradek
grade1
grade2
grade3
grade4
grade5
grade6


I need the script to delete the folder for the current user of the 7 accounts
if grade1 or any of the other 7 accounts are logged on:

delete:
users/grade1/library

copy:
cp users/shared/scripts/startup/library to users/grade1/

ownership:
chown everyone read and right to user/grade1/library


It's very important that the script only deletes the library for those 7 users and not the other accounts on the machine.
Is this possible? can someone help me put this together?

Thanks in advanced.

osx 10.6.8



kryten2
Jun 14, 2012, 08:56 AM
Info : http://hints.macworld.com/article.php?story=2006081701162739
http://www.sns.ias.edu/~cjm/mac-os-x-login-and-logout-scripts-demystified/
http://codesnippets.joyent.com/posts/show/1763
Plenty of info to be found on Google. Note that LoginHook and/or LogoutHook run as root!
It shouldn't be too hard to put this together yourself. You've already got your actions eg delete(rm), copy(cp), chmod. The first link might also give an anwser to your other post about Login log.

chown -- change file owner and group
chmod -- change file modes or Access Control Lists

If your not comfortable with Shell scripting you can use Applescript like so :

#!/bin/bash
/usr/bin/osascript "/path/to/your/script/LogoutDialog.scpt"

or

#!/bin/bash

/usr/bin/osascript <<-EOF

tell application "System Events"
activate
display dialog "Hello world"
end tell

EOF

h1068
Jun 15, 2012, 01:29 AM
Everywhere iv read the logoutscript doesnt have an extension...could that be why this isnt working?
this is what i tried


#!/bin/bash
/usr/bin/osascript "/usr/local/bin/logoutscript"

tell application "Finder"

do shell script "rm -R /users/grade1/Library"
end tell

tell application "Finder"
do shell script "cp -R /users/shared/library /users/grade1/"
end tell

tell application "Finder"
do shell script "chown -R grade1 /users/grade1/Library"
end tell





#!/bin/bash

/usr/bin/osascript <<-EOF



tell application "Finder"

do shell script "rm -R /users/grade1/Library"
end tell

tell application "Finder"
do shell script "cp -R /users/shared/library /users/grade1/"
end tell

tell application "Finder"
do shell script "chown -R grade1 /users/grade1/Library"
end tell



eof



sudo chown 755 /usr/local/bin/logoutscript

followed by

sudo defaults write com.apple.loginwindow LogoutHook /usr/local/bin/logoutscript

kryten2
Jun 15, 2012, 09:32 AM
Seems like you're blindingly copying code without knowing what you're doing.
1) If you want to change permissions on your logoutscript you use chmod NOT chown eg chmod 755 /usr/local/bin/logoutscript.

-- All my statements in an Applescript
-- Save it with meaningful name eg logoutscript.scpt
-- Note : script is NOT tested. I'm not responsible yada yada etc...
-- Example how to use list of users
set the_user to do shell script "logname"
set the_user_list to {"gradek", "grade1", "grade2", "grade3", "grade4", "grade5", "grade6"}
if the_user is in the_user_list then
-- You don't need a tell Finder block for the do shell script statements
--tell application "Finder"
--example of the use of the_user variable
do shell script "rm -R /users/" & the_user & "/Library"
do shell script "cp -R /users/shared/library /users/grade1/"
do shell script "chown -R grade1 /users/grade1/Library"
--end tell
end if

#!/bin/bash
# I'm telling osascript to run the Applescript I've made and saved in Script Editor
/usr/bin/osascript "/path/to/your/script/logoutscript.scpt"

or

#!/bin/bash
# I'm doing it differently and decided to put my Applescript statements in the shell script

/usr/bin/osascript <<-EOF

--tell application "Finder"
do shell script "rm -R /users/grade1/Library"
do shell script "cp -R /users/shared/library /users/grade1/"
do shell script "chown -R grade1 /users/grade1/Library"
--end tell

EOF

2) If you do your sudo defaults write command what do you see when you do sudo defaults read. What do you see when you do echo $PATH. Is /usr/local/bin in your PATH. If not your shellscript will not execute.

3) Does the folder /users exist? Shouldn't that be /Users.

4) You might run into trouble with spaces. Read this : http://developer.apple.com/library/mac/#technotes/tn2065/_index.html

A working example :

My LogoutHook :

sudo defaults write com.apple.loginwindow LogoutHook "/Users/test/logout"

Contents of the logout shellscript :

#!/bin/bash
/usr/bin/osascript "/Users/test/Documents/Applescripts/LogoutDialog.scpt"

Contents of the LogoutDialog.scpt Applescript. Nothing fancy it just deletes a folder named Postscript located on my Desktop and shows a dialog box at the end. It also does some logging to see what the script did.

set commonScript to load script alias "LeopardFirewire:Users:test:Library:Scripts:Common code.scpt"
set the_user to do shell script "logname"
set the_user_list to {"test"}
if the_user is in the_user_list then
try
do shell script "rm -R /Users/" & the_user & "/Desktop/Postscript"
log_event("Removed the Postscript directory from the Desktop of user : " & the_user) of commonScript
on error theErr
log_event("Error occured : " & theErr) of commonScript
end try
end if
tell application "System Events"
activate
display dialog "The logout script has finished if you see this dialog. Check ~/Library/Logs/Logoutscript-events.log. The dialog will close after 15 seconds." with title "Logout Dialog" with icon caution giving up after 15
end tell


I logged out 2 times. The first time it removed the Postscript folder, the second time it errored because the Postscript folder didn't exist anymore.

The output from the log :

2012-06-15 20:24:43 Removed the Postscript directory from the Desktop of user : test
2012-06-15 20:25:55 Error occured : rm: /Users/test/Desktop/Postscript: No such file or directory

In the Console application in All Messages I see the following for

The first line in my log :

15/06/12 20:24:45 /System/Library/CoreServices/System Events.app/Contents/MacOS/System Events[1243] OpenScripting.framework - 'gdut' event accepted from process "osascript" (pid=1238 uid=0 euid=0) with different owner than (uid=501 euid=501)
15/06/12 20:25:00 com.apple.loginwindow[1107] «class bhit»:, «class gavu»:true

The second line in my log :

15/06/12 20:25:56 /System/Library/CoreServices/System Events.app/Contents/MacOS/System Events[1315] OpenScripting.framework - 'gdut' event accepted from process "osascript" (pid=1310 uid=0 euid=0) with different owner than (uid=501 euid=501)
15/06/12 20:26:11 com.apple.loginwindow[1248] «class bhit»:, «class gavu»:true

h1068
Jun 16, 2012, 10:57 PM
Sweet! it works.
Thank you!!!!